#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11

    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?
    Thanks,

    Jonathan Cobb
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    C or C++ preferred?

    For C, the stdio function gets() will get a single line of the text file:
    Code:
    char* check ;
    check = gets( array[line] ) ;
    if( check != NULL )
    {
        /* success */
    }
    else
    {
        /* fail */
    }
    The sizeof array[line] must be large enough, otherwise the function will crap on your data.

    In C++ use the <iostream> ifstream class with the getline() member function. This is safer than gets() since it has a maximum length parameter.
    http://www.cplusplus.com/ref/iostream/ifstream/

    BTW blank spaces are characters as well! ASCII 32(0x20). There is no distinction as far as the code is concerned.

    Clifford
    Last edited by clifford; September 14th, 2003 at 04:08 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    13
    gets() shouldn't be used, use fgets() instead.

    Read this for a good explanation.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally posted by damonbrinkley
    gets() shouldn't be used, use fgets() instead.

    Read this for a good explanation.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Sorry, I meant fgets() of course :o . gets() gets a line fron stdin (usually the console, but may also be a redirected file).

    Clifford
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11
    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
    XO X
    XO XX
    X X O
    X

    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 12:57 AM.
    Thanks,

    Jonathan Cobb

IMN logo majestic logo threadwatch logo seochat tools logo