473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I input a text file to a linked list?

41 New Member
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 is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

Here is one of my sample code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct link
  7. {
  8.     int data;
  9.     link* next;
  10. };
  11.  
  12. class linkedlist
  13. {
  14. private:
  15.     link* first;
  16. public:
  17.     linkedlist()
  18.     {
  19.         first = NULL;
  20.     }
  21.     void additem(int d);
  22.     void display();
  23. };
  24.  
  25. void linkedlist::additem(int d)
  26. {
  27.     link* newlink = new link;
  28.     newlink -> data = d;
  29.     newlink -> next = first;
  30.     first = newlink;
  31. }
  32.  
  33. void linkedlist::display()
  34. {
  35.     link* current = first;
  36.     while( current != NULL)
  37.     {
  38.         cout << current->data << endl;
  39.         current = current->next;
  40.     }
  41. }
  42.  
  43. int main()
  44. {    
  45.     ifstream inputFile("names.txt");
  46.  
  47.     linkedlist li;
  48.  
  49.     li.additem("names.txt");
  50.     li.display();
  51.     return 0;
  52. }
  53.  
  54.  
As you see in the main part I'm not sure how to implement the text file into the linked list.
thank you very much in advance
Nov 4 '07 #1
1 8625
gpraghuram
1,275 Recognized Expert Top Contributor
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 is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

Here is one of my sample code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct link
  7. {
  8.     int data;
  9.     link* next;
  10. };
  11.  
  12. class linkedlist
  13. {
  14. private:
  15.     link* first;
  16. public:
  17.     linkedlist()
  18.     {
  19.         first = NULL;
  20.     }
  21.     void additem(int d);
  22.     void display();
  23. };
  24.  
  25. void linkedlist::additem(int d)
  26. {
  27.     link* newlink = new link;
  28.     newlink -> data = d;
  29.     newlink -> next = first;
  30.     first = newlink;
  31. }
  32.  
  33. void linkedlist::display()
  34. {
  35.     link* current = first;
  36.     while( current != NULL)
  37.     {
  38.         cout << current->data << endl;
  39.         current = current->next;
  40.     }
  41. }
  42.  
  43. int main()
  44. {    
  45.     ifstream inputFile("names.txt");
  46.  
  47.     linkedlist li;
  48.  
  49.     li.additem("names.txt");
  50.     li.display();
  51.     return 0;
  52. }
  53.  
  54.  
As you see in the main part I'm not sure how to implement the text file into the linked list.
thank you very much in advance

Hi,
If u want to store every word as one node then you have to write the code to read every word and store it to the list.
Your way of adding the filw eont help.

Raghuram
Nov 5 '07 #2

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

Similar topics

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...
0
by: hellosibba | last post by:
Hi i am trying to use text file to write a value as soon as there is any update/insert into a table. for that i am using a text file and made a linked server relation with it..... and inserting...
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
4
by: paranoidandroid | last post by:
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:
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...
60
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.