September 13th, 2003, 05:24 PM
reading input files
Anyone have any hints as to how to read an input txt file and store all of the characters as well as the blank spaces into an array. I already have the array allocated but need to figure out which input function to use to get all of the characters, and the blank spaces. then check to see if there is an endl or an int so as to end the array,or move to the next row in the 2d array for the endl. Any hints?
September 14th, 2003, 05:05 AM
C or C++ preferred?
For C, the stdio function gets() will get a single line of the text file:
The sizeof array[line] must be large enough, otherwise the function will crap on your data.
char* check ;
check = gets( array[line] ) ;
if( check != NULL )
/* success */
/* fail */
In C++ use the <iostream> ifstream class with the getline() member function. This is safer than gets() since it has a maximum length parameter.
BTW blank spaces are characters as well! ASCII 32(0x20). There is no distinction as far as the code is concerned.
Last edited by clifford; September 14th, 2003 at 05:08 AM.
September 14th, 2003, 07:20 AM
September 14th, 2003, 11:16 AM
Sorry, I meant fgets() of course . gets() gets a line fron stdin (usually the console, but may also be a redirected file).
September 15th, 2003, 10:37 PM
Thank you for the responses. As it turns out, The professor thought of those already and decided that would be too easy. It seems i will have to implement the read() function so i can check to see if i need to fill in any blanks towards the end of the line. the final product needs to look like a tic tac toe board. If what i am thinking is correct, i will have to read every character, space, and endl so as to see that every row of my 2d array will have the same number of columns terminated by the /n such that when i output it will automatically detect the /n and return. Does this make sense or is there an easier way to do it? the input file will look as follows:
4 5 //rows and columns
X X O
And i have to get 4 rows and five columns out of that. Anyone want to check my logic and verify that i'm on the right track?
Last edited by jonnyfive; September 16th, 2003 at 01:57 AM.