Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > load a txt file and linked list

Reply
Thread Tools

load a txt file and linked list

 
 
Kay
Guest
Posts: n/a
 
      08-29-2004
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 ?

 
Reply With Quote
 
 
 
 
lallous
Guest
Posts: n/a
 
      08-29-2004
Hello,

"Kay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      08-29-2004
In article <(E-Mail Removed)>,
Kay <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM



Advertisments