Thread: Reading from file question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Reading from file question

    i try to output this information later in the file...it works fine when i try to output it directly after these few lines of code, but near the end of the program I am getting garbage for the student first name...any suggestions or possible solutions?

    PS. I think this is my first post. Go me.


    Code:
     
    
    int i=0;
    numberofstudents=0;
    
    rfile>>student[0].id;
    
    
    while(i<SIZE && (!rfile.eof()))
    {
    
    rfile>>student[i].firstname;
    
    rfile>>student[i].lastname;
    rfile>>student[i+1].id;
    numberofstudents++;	
    
    
    i++;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Apparently missing are the most important parts, but I'd guess if you had an output stream that you might want to use << instead of >>.

    Now go to the FAQ and find out why not to control a loop via eof().
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    My instructor said we had to base it on eof, or until it reaches 75 students...but he's not very helpful

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Imnotready
    My instructor said we had to base it on eof, or until it reaches 75 students...but he's not very helpful
    Tell your instructor to visit the FAQ too.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In this case the eof() issue in the FAQ should not be a problem, since the id is read before the loop starts and again at the end. Also, the >> is fine here, since that code is inputting.

    Still, using eof() to control the is not ideal. The best way to input until the end of the file is to use the return value of the read operation to find out when the read fails. Something like this:
    Code:
    while(i<SIZE && (rfile>>student[i].id))
    The benefit of that is that it also finds read errors in addition to the end of the file.

    What does the input file look like around the point at which the output becomes garbage?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Imnotready
    i try to output this information later in the file...it works fine when i try to output it directly after these few lines of code, but near the end of the program I am getting garbage for the student first name
    Quote Originally Posted by Daved
    In this case the eof() issue in the FAQ should not be a problem, since the id is read before the loop starts and again at the end. Also, the >> is fine here, since that code is inputting.
    Sorry, as usual I read that wrong -- as the attempt to output.

    [I understand your point Daved, but since it always causes more problems than it's worth, why defend eof()?]

    ::shrinks away::
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I understand your point Daved, but since it always causes more problems than it's worth, why defend eof()?

    I'm not defending it (notice my suggestion on how to change it anyway). I just don't think it is causing a problem in this case and often instructors will teach students to use it "correctly" in a way that avoids the problem discussed in the FAQ. Since it will work and the instructor is teaching it that way, I would focus on what is causing the actual problem here.

    One of the problems with eof() is how easy it is to use incorrectly. I even had a response written up about how I thought it might be causing the OP's error before I looked at the code again and realized it probably wasn't. It certainly would be nice if the practice of using eof() correctly or incorrectly was moved away from.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    More in depth:


    here are what the files look like


    roster.txt file

    0 Jason Jacobs
    1 Alyson Andrews
    2 Doug Daniels
    3 Karen Karnes


    grades.txt file <--First number=student id, second number= test id, 3rd number=test score.

    0 1 83
    0 2 91
    0 3 79
    2 1 62
    3 2 89
    2 2 91
    2 3 70
    3 1 57
    3 3 84








    Code:
     
    
    using namespace std;
    #include "studenttype.h"
    const int SIZE=75;
    
    
    int main ()
    
    {
    	studenttype student[SIZE];
    	ifstream gfile, rfile;
    	string rfname, gfname;
    	int read, read2, numberofstudents;
    	cout<<"Please enter the name of the roster file: ";
    	getline(cin, rfname);
    	rfile.open(rfname.c_str()); 
    
    
    
    
    	int i=0;
    	numberofstudents=0;
    
    	rfile>>student[0].id;
    
    
    	while(i<SIZE && (!rfile.eof()))
    	{
    
    		rfile>>student[i].firstname;
    
    		rfile>>student[i].lastname;
    		rfile>>student[i+1].id;
    		numberofstudents++;	
    
    
    		i++;
    	}
    
    	cout<<"Processing roster file...found "<<numberofstudents<<" students."<<endl;
    
    	cout<<student[0].id<<" "<<student[0].firstname<<" " <<student[0].lastname<<endl;
    
    	cout<<student[1].id<<" "<<student[1].firstname<<" " <<student[1].lastname<<endl;
    
    	cout<<student[2].id<<" "<<student[2].firstname<<" " <<student[2].lastname<<endl;
    
    	cout<<student[3].id<<" "<<student[3].firstname<<" " <<student[3].lastname<<endl;  
    
    	
    
    	cout<<"Please enter the name of the grades file: ";
    	getline(cin, gfname);
    	gfile.open(gfname.c_str()); 
    
    	
    
    	int ctr=0;
    	
    	gfile>>read;
    	while(ctr<225 && !gfile.eof())
    	{
    		gfile>>read2;
    		student[read].testid[read2];
    		gfile>>student[read].testscore[read2];
    		
    		cout<<student[read].lastname<<", "<<student[read].firstname<<endl; 
                    //here is where im having trouble outputting the code correctly
    			
    		ctr++;
    		gfile>>read;
    	
    
    	
    	}

    again any help at all is appreciated...i've got a while before this is due, i just wanted to get a jump and i've been stuck on this all night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM