| | |
text file to linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
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())
{
}
}
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.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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.
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:
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:
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.
C++ Syntax (Toggle Plain Text)
while(!inFile.eof()) { }
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
employee anEmployee; inFile >> anEmployee.ID; inFile >> anEmployee.name; inFile >> anEmployee.performance; inFile >> anEmployee.curSalary; anEmployee.next = NULL; head = &anEmployee; 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
![]() |
Similar Threads
- Linked List Problem (C++)
- Linked list help (C++)
- how to read a text file backwards? (C)
- Text file parser in C (C)
- ordered linked list (C)
- *strcpy and linked list (C#)
- struct linked list problem (C)
- How to read in a sentence and insert in to linked list? (C++)
Other Threads in the C++ Forum
- Previous Thread: Access violation
- Next Thread: reading a float to an int variable crashes me
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






