473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Input from file to linked list

I have a txt file with a list of records for example:

John Swift 15 2005 20000
Andrew Smith 25 2001 25000

I am trying to read in this data into a linked list of data structures as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.         temp = new client;
  4.         head = temp;
  5.         //do
  6.         while(!infile.eof())
  7.             {
  8.                 infile >> temp->name;
  9.                 infile >> temp->experience;
  10.                 infile >> temp->year;
  11.                 infile >> temp->desiredsalary;
  12.  
  13.                 temp->next = new client;
  14.                 temp = temp->next
  15.  
  16.             } 
  17.  
  18.  
  19.             temp->next = NULL; // set last next in the linked list to NULL
  20.             temp = head; // move temp pointer to start of the list
  21.         }
  22.  
  23.  
The problem I'm having is that as I have shown in the text file, the name has a whitespace in between name and surname.

I'm not very good with input / output and strings. Could someone please give me an idea as to how to enter both name and surname from the file into one variable in the structure of the linked list.

I think the getline command has to be used:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  infile.getline (name);
  3.  
  4.  
I don't know how to set up delimiters to do this... any help greatly appreciated

Thx!
Mar 4 '08 #1
4 5065
weaknessforcats
9,208 Expert Mod 8TB
I assume you know the format of the file to be read in.

That is, the name has to be the same length in each record or you can't set the getline argument for the number of characters to read (the buffer size).

Otherwise, the >> operator stops on whitespace so you only get one word of the name. If you know the name always has two words you can use the >> twice and fetch one word at a time.

Also, I would separate your file I/O from your linked list.

And lastly, since you are using C++ why are you re-coding a linked list rather than using the linked list container in the STL?
Mar 4 '08 #2
I assume you know the format of the file to be read in.

That is, the name has to be the same length in each record or you can't set the getline argument for the number of characters to read (the buffer size).

Otherwise, the >> operator stops on whitespace so you only get one word of the name. If you know the name always has two words you can use the >> twice and fetch one word at a time.

Also, I would separate your file I/O from your linked list.

And lastly, since you are using C++ why are you re-coding a linked list rather than using the linked list container in the STL?

Hi!

Thanks for your comments, appreciate your help.

Unbfortunately the names vary both in actual length and the number of names given hence the difficulty.

I'm recoding a linked list as this is how I am being asked to do this, that's one of the constraints as is the ability to use classes.

Just to follow up on what you suggested about seperating the file i/o from linked list...why do you say that?


Regards...
Mar 4 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
[paranoidandriod]
Just to follow up on what you suggested about seperating the file i/o from linked list...why do you say that?
[/quote]

OK. Don't separate them. Let's say you spend 500 hours getting the code to work. Next week you need a linked list in another program that does not require a file. Now what?

You can't use the code you have already written because it is intertwined with file I/O. So, you make a copy, delete the file I/O stuff and end up with two copies of the code to maintain. Then a third situation arises, so you copy the code again...

Whenever you write code keep an eye open for reuseability. By separating the linked list from the file I/O you have linked list capability in any program by using the original debugged code.
Mar 4 '08 #4
Thanks...that makes a lot of sense.
Mar 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: fumanchu | last post by:
I've got to let end users (really just one person) load billing batch files into a third party app table. They need to specify the billing cycle name, the batch name, and the input file name and...
5
by: moostafa | last post by:
Hi, I'm writing a program that performs arithmetic operations on integers. I want to be able to type in a bunch of integers seperated by any amount of white space then terminate input with a...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
1
by: d0ugg | last post by:
Hello everyone, I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that. my text file looks like this- (the name of the text file...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
11
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.