reading and storing strings

This is a discussion on reading and storing strings within the C++ Programming forums, part of the General Programming Boards category; i am trying to read a string from a file, store it in an array, then move on to the ...

  1. #1
    Unregistered
    Guest

    reading and storing strings

    i am trying to read a string from a file, store it in an array, then move on to the next line of the file and repeat.
    i guess i just can't figure out the loop that will do it.
    thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Doesn't anyone write their own code anymore?

    The basic mechanism is:
    open a file, count the lines. Create an array of this many elements. Rewind the file pointer( like: rewind(filepointer); ).
    Then:
    while(filepointer != NULL)
    {
    fgets(array[i],100, filepointer);
    i++;
    }
    Now close the file pointer.
    That's it.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Unregistered
    Guest
    thanks for the help.
    but, look, i am just beginning. i don't want the code written for me. some friendly, helpful advice would do.
    i thought maybe i could learn something from someone more learned in the field than i.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Mmm - Sebastiani's code is considerably wrong, but vVv's code is much better

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Salem, I have used this code hundreds of times with success, you should know it is not 'wrong'!! And yet I agree that vVv's code is much more efficient, still his code does not copy to an array of strings...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Unregistered
    Guest
    There is no one way to read files, or write to files for that matter. In general terms, in C++ you may use either streams or FILE *'s to deal with files (in C you can only use FILE *.s). You can read or write one char at a time, on struct/instance of a class at a time, one line at a time, one phrase terminated by a specific char at a time, etc. This flexibility is both good (provides you with more options) and bad (means you have to choose how you want to do it).

    If you define a string as a series of characters ending in a NULL char then using the istream >> opoerator or the istream method getline() are common tools to use. As a word of warning, there is a special consideration if you mix use of the >> operator for reading some information before using the getline() method in your program. Specifically, you should call the ignore() method of istreams after the (at least the last) call to >> before the call to getline(). You can read about streams, >>, <<, getline(), ignore() in a reasonable textbook, by browsing through the multiple posts on the topic here on this board, or by asking specific questions.

    Here's a general schema similar to what you might find as an example in a textbook:

    char fileName[] = //enter name of file with extensionto use with this program

    //open and ifstream and associate the file with it
    ifstream fin(filenane);

    //check to be sure file opened successfully here, I didn't do it

    char word[1000][21];//an array to hold 1000 words up to 20 char each

    int i = 1;

    //read in first word;
    fin >> word[0]

    //read in first 1000 words or until fin fails for whatever reason
    while(fin && i < 1000)
    {

    //read in remainder of words, one at a time.
    fin >> word[i++];
    }

    //close the ifstream
    fin.close();

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > I have used this code hundreds of times with success
    Not a chance

    > while(filepointer != NULL)
    What the hell is this?

    fgets does not MODIFY filepointer, so there's no point in testing to see whether its NULL or not.

    It will be NULL if you failed to open the file in the first place, but assuming you got past that, it would remain non-NULL thereafter.

    fgets on the other hand, is what returns non-NULL when it reads something, and NULL when it hits the end of file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Replies: 5
    Last Post: 04-26-2007, 08:50 AM
  3. Storing and reading program settings
    By publikum in forum Windows Programming
    Replies: 3
    Last Post: 01-25-2005, 10:22 AM
  4. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21