Reading multiple lines from a txt file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 5
Reputation: bapef is an unknown quantity at this point 
Solved Threads: 0
bapef's Avatar
bapef bapef is offline Offline
Newbie Poster

Reading multiple lines from a txt file

 
0
  #1
Jul 11th, 2007
ok here is an example of the type txt file I am trying to read
12345678901234567890123456789012345678901234567890
Bugs Bunny Jr. 1234 1001.01
Dr. Wiley Coyote 2345 1002.02
Taco Speedy Gonzales3456 1003.03
Billy the Goat 4567 1004.04
Porky Pig 5678 1005.05

what I am trying to do is skip the first line and then read the rest storing it in a linked list of structs the problem I am running into is that it will get all the way to the third line then skip the fourth read the fifth and get junk for the sixth.

here is the read function I am using all variables are global

void readfilefn()
{
firstcustomer=new customerType;
fin.ignore(50,'\n');
fin.get(firstcustomer->name, 21);
currcustomer=firstcustomer;
fin>>currcustomer->pin;
fin>>currcustomer->balance;
firstcustomer->link=currcustomer;
//check if the file is being read properly
cout<<"\nCustomer Name: "<<firstcustomer->name<<endl;
cout<<"Pin : "<<firstcustomer->pin<<endl;
cout<<"Balance : "<<firstcustomer->balance<<endl;

counter++;

while(!fin.eof()&&fin.peek()=='\n')
{
newcustomer=new customerType;
int row=0;
while(row<counter)
{
fin.ignore(50,'\n');
row++;
}
fin.get(newcustomer->name, 21);
fin>>newcustomer->pin;
fin>>newcustomer->balance;
currcustomer->link=newcustomer;
currcustomer=newcustomer;
cout<<"\nCustomer Name: "<<currcustomer->name<<endl;
cout<<"Pin : "<<currcustomer->pin<<endl;
cout<<"Balance : "<<currcustomer->balance<<endl;
//if(fin.peek()!='\n')fin.ignore();
counter++;
}//end of while


cout<<"There are "<<counter<<" accounts"<<endl;

}
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Re: Reading multiple lines from a txt file

 
0
  #2
Jul 11th, 2007
Hi mate,

I didn't read through all the source code, but basically you can use getline to do this.

getline has the following parameters:
std::getline ( source stream , desination stream , delimeter )

There are several ways to use this..

e.g.
  1. ...
  2. std::string temp, total;
  3. while (!file.eof())
  4. {
  5. std::getline(file,temp); // delimeter is the newline char by default
  6. total += temp;
  7. }

or something like this:

  1. ...
  2. while(std::getline(file,temp))
  3. str += temp;

I shouldn't be giving away the solution so cheaply, but it will be something like this:
  1. getline(file,str);
  2.  
  3. for (int counter = 0; !file.eof(); counter++)
  4. {
  5. getline(file,someKindOfString);
  6. // do whatever else with string
  7. }
  8.  
  9. cout << counter << "accounts";
Last edited by phalaris_trip; Jul 11th, 2007 at 6:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: bapef is an unknown quantity at this point 
Solved Threads: 0
bapef's Avatar
bapef bapef is offline Offline
Newbie Poster

Re: Reading multiple lines from a txt file

 
0
  #3
Jul 11th, 2007
Thank you for your response I figured it out, the initialization of the variable row should have been outside of the outter while loop.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Reading multiple lines from a txt file

 
0
  #4
Jul 11th, 2007
Originally Posted by phalaris_trip View Post
  1. ...
  2. while(std::getline(file,temp))
  3. str += temp;
Of the 3 solutions you posted, this is the only one which won't crash and burn once the file hits EOF. the EOF flag is not set until after the stream reaches EOF (A read is attempted and fails) - which means your final call to getline will break, causing problems for the final iteration of the loop.

The loop in this example doesn't rely on EOF, so it works well.

As a general rule of thumb, you should never use EOF as a loop's terminating condition when reading from a file.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Reading multiple lines from a txt file

 
0
  #5
Jul 11th, 2007
The problem probably is at Speed Gonzales3456. There is no space between es and 3456, so afterwards everything is out of sync by one field.

You last post wasn't visible to me until I posted this one
Last edited by Tight_Coder_Ex; Jul 11th, 2007 at 9:45 pm. Reason: Other post showed up within seconds of mine
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC