Thread: A problem with reading multiple lines from a file

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

    A problem with reading multiple lines from a file

    Hello, i have a file that holds details about movies and i want to read these details into an array in my program. The file layout is this:

    1 //number of films recorded
    Pulp Fiction //title
    Tarantino //director
    5 //current position
    100 //total length

    in my program i have the following code to read in this data:

    Code:
    fin >> numOfMP3s;
    
    while (count !=numOfMP3s)
    {
    	fin.getline(title,50);
    	fin.getline(director,50);
    	fin >> currentPosition;
    	fin >> length;
    	cout << length;
    }
    the number of mp3s is read in correctly(first line in the file) however the others are not. when i cout title or director nothing is displayed and when i cout either of the integer values i get the value -858993460 printed out.

    any ideas? the only tutorials i could find deal with just simple file reading.

    thanks in advance

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you'd have better luck using terminating characters instead of fixed-length arrays:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
    	int film;
    	std::string title;
    	std::string director;
    	int pos;
    	int len;
    	
    	std::fstream file("test.dat",std::ios::in);
    	while(file>>film)
    	{
    		file.ignore(32000,'\n');
    		getline(file,title,'/');
    		file.ignore(32000,'\n');
    		getline(file,director,'/');
    		file.ignore(32000,'\n');
    		file>>pos;
    		file.ignore(32000,'\n');
    		file>>len;
    		file.ignore(32000,'\n');
    
    		std::cout<<"-- Film "<<film<<" --"
    			<<"\nTitle:\t\t\t"<<title
    			<<"\nDirector:\t\t"<<director
    			<<"\nCurrent Position:\t"<<pos
    			<<"\nTotal Length:\t\t"<<len<<std::endl;
    	}
    	file.close();
    	file.clear();
    
    	return 0;
    }
    it is just simple file reading

    edit: oh yeah, I copied that file you gave verbatim, so if those comments aren't in there, you may need to modify my method... or maybe not...yeah, you will.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You read in the number of mp3s with fin >>, but that leaves a newline in the stream from the end of that line. When you call getline next, it stops at the first newline (the one at the end of the number of mp3's line. The simple solution is to call fin.ignore() after you use fin >> to ignore the newline at the end of that line. (This assumes that your comments aren't actually in the input file.)

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    hi. thanks for the help so far, however i am still getting exactly the same problem.

    my code now looks like this:

    Code:
    			fin.ignore(32000,'\n');
    			fin.getline(title,50);
    			fin.ignore(32000,'\n');
    			fin.getline(director,50);
    			fin.ignore(32000,'\n');
    			fin >> currentPosition;
    			fin.ignore(32000,'\n');
    			fin >> length;
    			fin.ignore(32000,'\n');
    			cout << length;
    the output results are exactly the same as above. any more ideas? (by the way the comments arent in my text file and i have tried playing around with the 32000 value using smaller ones as well as just trying fin.ignore() )

    thanks again
    Last edited by edd1986; 03-18-2006 at 10:58 PM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Read numOfMP3s, currentPosition and length as you did the other values (using getline() into a dummy string) and convert to integer.

    When you read
    1 //number of films recorded
    using
    fin >> numOfMP3s;
    you correctly read the "1", but " //number of films recorded" is still in the file ready to be read next. After all it isn't a number and could not be read into the variable.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    sorry should have made it clear in original post. the comments aren't actually in the text file - just the data

    EDIT: thanks it is working now
    Last edited by edd1986; 03-19-2006 at 07:37 AM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by edd1986
    sorry should have made it clear in original post. the comments aren't actually in the text file - just the data
    Yes, it helps to understand the problem....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading lines from a File
    By DivineSlayer936 in forum C++ Programming
    Replies: 12
    Last Post: 04-02-2007, 12:12 PM
  2. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 01:49 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM