Message Board
Message Board > C/C++ > help reading .txt files |
July 2, 2013, 04:22 | |
DoctorN
Whiskered 91 posts |
Code: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 Here is my .txt file Code: int lvlWidth; int lvlHeight; void create::render_level( string levelFile ) { //The width and height of the level lvlWidth = 20; lvlHeight = 15; //Create a 2D array based on the size of the level int** level; level = new int*[lvlWidth]; for (int i = 0;i<lvlWidth;i++) level[i] = new int[lvlHeight]; //Open the level ifstream lvlFile; lvlFile.open( levelFile.c_str() ); //If the level fails to open if( !lvlFile ) { cout << "File open failed!\n"; } int i = 0; int j = 0; char line[300]; //Read the file while( lvlFile.getline(line, sizeof line) ) { //cout << line << endl; for( j = 0; j < lvlWidth; ++j ) { sscanf( line+(j*2), "%d%*c", &level[i][j] ); } ++i; } //Place things on the level for( i = 0; i < lvlWidth; i++ ) { for( j = 0; j < lvlHeight; j++ ) { if( level[j][i] == 1 ) //Place a block when level[i][j] is not 0 { lvlbox[j*20+i].x = (i*16); lvlbox[j*20+i].y = (j*16); lvlbox[j*20+i].w = 16; lvlbox[j*20+i].h = 16; //If there is a place for a block, say where it is //cout << "lvlbox[" << ((j*20+i+1)) << "] is at coordinates: " << lvlbox[j*20+i].x << "," << lvlbox[j*20+i].y << endl; } } } //Close the file lvlFile.close(); //Set the way for objects to be placed initialized = false; } Here is the code that reads the level Code: for( int i = 0; i < lvlWidth; i++ ) { for( int j = 0; j < lvlHeight; j++ ) { SDL_FillRect( screen, &lvlbox[j*20+i], SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) ); } } Here is the code that renders it to screen. Now you may see lvlbox[j*20+i], just change it to lvlbox[j*lvlWidth+i]. I need help with the following: 1. counting the number of characters per line while excluding the comma (or counting the commas seperately and then subtracting the total by the number of commas, whatever is easier). I also need something to count the number of lines. The reason being is I want to be able to put the number of characters in lvlWidth and the number of lines in lvlHeight. 2. what if I decide to have double digits between the commas? like 01 instead of 1. how would I be able to do that? (going back to no. 1, how could i still put it into lvlWidth, just divide the total by 2 or something?) 3. I may be getting ahead of myself, but if I were to make "-end-" the last line of the .txt file, how could I make the code understand that (while not counting -end- as another line, because I would like to put variables after -end-)? Thanks ____________ |
# |
July 3, 2013, 06:21 | |
DoctorN
Whiskered 91 posts |
could you also show me how to count this? Quote: 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 thanks ____________ |
# |
July 10, 2013, 17:27 | |
DTM
Earthling! 823 posts |
I can't answer anything precisely because I'm too lazy. However here are some pointers. 1.) um, just count the commas? int commaCount = 0; for (int i = 0; line[i] != 0; i++) { if (line[i] == ',') commaCount ++; } The total number of numbers on a line is obviously commaCount plus one. 2) personally I would be using strtok or something http://www.cplusplus.com/reference/cstring/strtok/ split each line by commas, and then pass each sub string to some function to convert strings to numbers: http://www.cplusplus.com/reference/cstdlib/atoi/ 3) http://www.cplusplus.com/reference/cstring/strstr/ while iterating through the lines: if (strstr(line, "-end-") != 0) // "-end-" was found in the line string { ...do something different } There are a lot of other string functions too. Study them all. http://www.cplusplus.com/reference/cstring/ Or if you want to operate on proper C++ style string objects (rather than old school C arrays), you can use the methods of the String class: http://www.cplusplus.com/reference/string/string/ e.g. you might use String.find rather than strstr() http://www.cplusplus.com/refer … ng/string/find/ if (std::string(line).find("-end-") != std::string::npos) { // "-end-" was found } standard disclaimer applies that I don't really have a clue what you are doing since i didn't study it closely. ____________ :o |
# |
July 10, 2013, 17:36 | |
DTM
Earthling! 823 posts |
You also might look for a nice C++ library that has lots more utility functions. E.g. Boost. Boost has a "split" function which could be used for splitting by commas or whatever: http://www.boost.org/doc/libs/ … tring_algo.html http://www.boost.org/doc/libs/ … algo/usage.html also several different answers for splitting a line here: http://stackoverflow.com/quest … e-a-string-in-c [Edited on July 10, 2013 by DTM] ____________ :o |
# |
July 12, 2013, 19:23 | |
DoctorN
Whiskered 91 posts |
Thanks
____________ |
# |
Message Board > C/C++ > help reading .txt files