load a txt file and linked list

Discussion in 'C++' started by Kay, Aug 29, 2004.

  1. Kay

    Kay Guest

    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 ?
    Kay, Aug 29, 2004
    #1
    1. Advertising

  2. Kay

    lallous Guest

    Hello,

    "Kay" <> wrote in message
    news:...
    > 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
    lallous, Aug 29, 2004
    #2
    1. Advertising

  3. Kay

    Jon Bell Guest

    In article <>,
    Kay <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Aug 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    463
    emerth
    Jul 10, 2003
  2. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  3. Sameen
    Replies:
    2
    Views:
    421
    Victor Bazarov
    Aug 29, 2005
  4. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,468
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page