text file to linked list

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

Join Date: Jan 2008
Posts: 3
Reputation: vtskillz is an unknown quantity at this point 
Solved Threads: 0
vtskillz vtskillz is offline Offline
Newbie Poster

text file to linked list

 
0
  #1
Feb 12th, 2008
Hi
I was wondering if someone could help with linked list. I'm trying to read i a text file in this format:

ID Name PR Salary
339 GOERGE 4 26000
221 SANDY 4 22600
101 RONNY 3 35250

and put it in a linked list. I have this much so far.

struct employee
{
int ID;
char name[10];
int performance;
int curSalary;
employee *next;
};
int main()
{
ifstream inFile;
employee *head, *current = NULL;

inFile.open("InputFile.txt");
if (!inFile)
{
cerr << "Unable to open file datafile.txt" << endl;
exit(1); // call system to stop
}
inFile.getline(inFile,80);


while(!inFile.eof())
{


}
}
Last edited by vtskillz; Feb 12th, 2008 at 1:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: text file to linked list

 
0
  #2
Feb 12th, 2008
1) give some of your thoughts;
2) see the reading of the file and list working manuals
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3
Reputation: vtskillz is an unknown quantity at this point 
Solved Threads: 0
vtskillz vtskillz is offline Offline
Newbie Poster

Re: text file to linked list

 
0
  #3
Feb 12th, 2008
and where could i find these manuals
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: text file to linked list

 
0
  #4
Feb 12th, 2008
I think you have a decent start. There are links on this site that discuss some of the potential perils of using the loop control style that you used (using "eof") that you may want to look at.

  1. while(!inFile.eof())
  2. {
  3. }

Here is one of them. There are others: http://www.daniweb.com/forums/post155265-18.html

I would consider, since you know the format of your data file, changing this:
  1. inFile.getline(inFile,80);

to something that reads directly into your data members so you don't have to parse a string later (I don't think your getline command would work anyway since you have not provided a string where you want to store the data that you read in from the file). Something like this:

  1. employee anEmployee;
  2.  
  3. inFile >> anEmployee.ID;
  4. inFile >> anEmployee.name;
  5. inFile >> anEmployee.performance;
  6. inFile >> anEmployee.curSalary;
  7.  
  8. anEmployee.next = NULL;
  9. head = &anEmployee;
  10. current = &anEmployee;

That primes the loop with the first employee. It'll be your head node. Progressing inside the loop (again, consider changing the "eof" control condition part of it) you can read in the data in a similar method and adjust the "next" member of the previous employee read in from the file to point to the one that was just read in.
Last edited by VernonDozier; Feb 12th, 2008 at 8:32 pm. Reason: typo
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