what's a good way to read each line of a file into an array

This is a discussion on what's a good way to read each line of a file into an array within the C++ Programming forums, part of the General Programming Boards category; I have a file with an unkown number of lines. Each line is going to be red into an array ...

  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    [RESOLVED]what's a good way to read each line of a file into an array

    I have a file with an unkown number of lines. Each line is going to be red into an array of structs. What is the best way for doing this? For example do I have to go through the entire file once, just to determine the number of lines so I can make the array that size?
    Last edited by help_seed; 03-25-2010 at 01:17 PM. Reason: RESOLVED

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    No... Just use a std::vector and grow it as you go.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Assuming it's a pretty simple struct, this is the approach I would take...

    Code:
    ifstream input_stream ( "input_file.txt" );
    string input = "";
    vector <my_struct> input_objects;
    
    while ( !input_stream.eof() )
    {
    	getline(input_stream, input);
    	
    	...from here you can tokenize/parse the string "input"...
    	
    	input_objects.push_back( struct_you_created );
    }
    Some might take a different approach. I don't by any means call this the "right way" or the "only way".
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Overload operator>> for your struct and insert into vector as mentioned above. If done properly, the whole file processing and vector storing loop can usually be reduced to a couple lines:
    Code:
    while( input_file >> temp_struct )
      struct_vector.push_back(temp_struct);


    Quote Originally Posted by DavidP
    Code:
    while ( !input_stream.eof() )
    {
    	getline(input_stream, input);
    Do I really need to say it?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13
    Ok thanks, got it.
    If I want to take a field from a string and put it into a certain part of a struct, knowing the deliminator is a space, how would I do that?

    For example if the line I'm reading has the format
    "name callery id status" and I want to store the name in the section of the struct for it, etc.

    So in PHP it would look something like this
    PHP Code:
    $array explode(line" ")
    myStruct.name $array[0]
    myStruct.callery $array[1]
    myStruct.id $array[2]
    myStruct.status $array3 
    ...just so my questions clear
    in Java I'd use a scanner with space as the delimiter

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Space is the default delimiter for >>.

  7. #7
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13
    can you remind me how that works?
    for example if I know that the line starts with a number, and I wanted to read that into an int how would I do that?
    currentLine >> callery;

    oh, I figured it out, used a string stream
    Last edited by help_seed; 03-25-2010 at 01:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there any way to read a line from a text file?
    By megareix in forum C Programming
    Replies: 13
    Last Post: 01-09-2009, 12:13 PM
  2. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 11:41 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Read SPECIFIC line from text file
    By 13373ar5 in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 05:14 AM
  5. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 04:16 AM

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