473,497 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

load a txt file and linked list

Kay
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?

2) If I enter a number, can I use to call a particular node ?
eg enter a number: 3
calling node of number 3
is it possible ?

Jul 22 '05 #1
2 8455
Hello,

"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?

2) If I enter a number, can I use to call a particular node ?
eg enter a number: 3
calling node of number 3
is it possible ?


Please refer to:
1)File Streams
2)fstream.getline()
3)possibly <vector> to store variables

Your second question is not clear.

Hope that helps,
Elias
Jul 22 '05 #2
In article <41**************@yahoo.com.hk>,
Kay <er*********@yahoo.com.hk> wrote:
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?


getline() is normally used for reading an entire line, terminated by a
newline, but you can specify any terminator you like. So, to read the
first item (which looks like a name), you can do this (not a complete
program):

#include <ifstream>
#include <string>

using namespace std;

string name;

getline (infile, name, ';');

The next input operation resumes with the first character after the
';'. Therefore, you can read the rest of the data into separate strings
by repeating this trick as necessary, using the default terminator for the
last iten at the end of the line.

You probably want the numeric data (age?) to be an int instead of a
string. You can either convert the string that getline() gives you, into
an int by various techniques, or you can read it into an int in the first
place by taking some care:

string name;
int age;

getline (infile, name, ';');
infile >> age; // stops at the following ';'
infile.ignore (1000,';'); // skip past the ';'

Reading a single char also skips past the ';', but only if you can
guarantee that there's no extra whitespace between the number and the
semicolon.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4255
by: John C | last post by:
I am working with an HTML-based form that uses a select element that requires about 200 options. Is there a way that I can load, or select one of these options from a file, rather than hardcode...
5
859
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
5
3289
by: Scott Tilton | last post by:
I am having a terrible time getting this to work. I am hoping someone out there can help me with very specific code examples. I am trying to get the linked tables in my Access 97 database to be...
10
15097
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
4
3823
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
165
6742
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
1
15496
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
2493
by: leftfootfirst | last post by:
Hi all, I'm just going to get right down my question since the exact program details are unnecessary and complicated. Basically, I have a database handler that writes to a file certain...
4
5068
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:
45
4368
by: Dennis | last post by:
Hi, I have a text file that contents a list of email addresses like this: "foo@yahoo.com" "tom@hotmail.com" "jerry@gmail.com" "tommy@apple.com" I like to
0
7121
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
7162
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
7197
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...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
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
3088
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
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.