reading from text file

Discussion in 'C++' started by mahurshi@gmail.com, Sep 7, 2005.

  1. Guest

    i have a simple question guys

    i have to read a file that looks more or less like this


    AND 1 2 3
    OR 2 3 4
    INV 5 2
    BUF 7 1
    AND 1 2

    INPUT 1 2 3 4 -1
    OUTPUT 5 7 -1


    (i just made up some random numbers but i guess that gives an idea of
    the input file format)

    onw what i want to do is read this input into a structure (string name,
    int array of inputs, int one output .. the value of these ints being
    the values next to the logic gate)

    so for example

    name = AND, inputs[0] = 1, input[1] = 2, output = 3


    my question is ... how do i parse this file ? (i can do the rest
    myself)

    i know how to read the whole line using getline .. but it is making
    things too complicated for me as the input has both characters and
    integers and sometimes the number of integers is varying (e.g. INV is
    followed by only 2 numbers).. and I have to somehow split these fields.

    i can write this entire stuff in perl in about 3 or so lines (using =~
    pattern matching) but i'd really appreciate it if someone can give me
    an idea on how i can parse this file easily.


    thanks
    , Sep 7, 2005
    #1
    1. Advertising

  2. Steve Guest

    On 7/9/05 22:34, in article
    , ""
    <> wrote:

    > i can write this entire stuff in perl in about 3 or so lines (using =~
    > pattern matching) but i'd really appreciate it if someone can give me
    > an idea on how i can parse this file easily.


    Read up on the std::istream operator >>

    --
    Regards,
    Steve

    "...which means he created the heaven and the earth... in the DARK! How good
    is that?"
    Steve, Sep 7, 2005
    #2
    1. Advertising

  3. wrote:
    > i have to read a file that looks more or less like this
    >
    >
    > AND 1 2 3
    > [..]
    >
    > onw what i want to do is read this input into a structure (string name,
    > int array of inputs, int one output .. the value of these ints being
    > the values next to the logic gate)
    >
    > so for example
    >
    > name = AND, inputs[0] = 1, input[1] = 2, output = 3
    >
    >
    > my question is ... how do i parse this file ? (i can do the rest
    > myself)
    >


    Read a line into a string. Define an istringstream from that string.
    Read another string and then integer numbers from that istringstream
    until the end of the istringstream. The string will have your word,
    the number will contain the running thing. You can branch based on
    the word as soon as you have it. If the number of integers is not
    known, read the first, then if more, push it into a vector<int> and
    read the other. Repeat until empty. You will have a vector<int> with
    all integers less the last one, and you will have just read the last
    number. Or just keep pushing and then extract the last one...

    V
    Victor Bazarov, Sep 7, 2005
    #3
  4. writes:

    > i can write this entire stuff in perl in about 3 or so lines (using =~
    > pattern matching)


    In addition to what others have said, if you're interested in using regular
    expressions and pattern matching, you might have a look at the regex classes
    included with boost:

    <http://www.boost.org>

    Those classes aren't part of the official standard yet, but they're on the
    list of additions being discussed for future versions of the standard.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Sep 7, 2005
    #4
  5. Guest

    Thanks for the info guys.

    I am gonna look up and see if I get get somewhere with this, otherwise
    I will just bail out into using perl :) (my prof believes it is v.
    easy in c/c++ ... well, we'll see)

    And if I have questions, I will post them here (gimme a couple of days
    to figure eveyrthing out)

    Thanks
    , Sep 8, 2005
    #5
  6. wrote:
    >
    > Thanks for the info guys.
    >
    > I am gonna look up and see if I get get somewhere with this, otherwise
    > I will just bail out into using perl :) (my prof believes it is v.
    > easy in c/c++ ... well, we'll see)


    It *can* be made very easy and it *can* be made very complicated.
    It all depends on what level you want to guard your program
    against file format errors.

    A simple solution would be:

    open file
    check for file open errors

    std::string type;
    int Input[20];
    int Output;

    while( file >> type ) {

    if( type == "AND" || type == "OR" )
    file >> Input[0] >> Input[1] >> Output;

    if( type == "INV" )
    file >> Input[0] >> Output;

    ...
    }

    The problem with this code is, that it lacks any sort of handling
    input format errors. As long as the file is perfect, it works perfectly.
    At the moment someone made a mistake in the file, it all screws up.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 8, 2005
    #6
  7. Jim Langston Guest

    <> wrote in message
    news:...
    >i have a simple question guys
    >
    > i have to read a file that looks more or less like this
    >
    >
    > AND 1 2 3
    > OR 2 3 4
    > INV 5 2
    > BUF 7 1
    > AND 1 2
    >
    > INPUT 1 2 3 4 -1
    > OUTPUT 5 7 -1
    >
    >
    > (i just made up some random numbers but i guess that gives an idea of
    > the input file format)
    >
    > onw what i want to do is read this input into a structure (string name,
    > int array of inputs, int one output .. the value of these ints being
    > the values next to the logic gate)
    >
    > so for example
    >
    > name = AND, inputs[0] = 1, input[1] = 2, output = 3
    >
    >
    > my question is ... how do i parse this file ? (i can do the rest
    > myself)


    Maybe this will help:
    //////////////////////
    // Particle Streams //
    //////////////////////
    int PSNumber = 0;
    std::ifstream PSFile("data/PS.DAT");
    if (PSFile.is_open())
    {
    while (! PSFile.eof() )
    {
    CPSData PSBuffer;
    std::string FileName;
    if ( PSFile >> PSBuffer.x >> PSBuffer.y >> PSBuffer.z >> FileName )
    {
    FileName = "data/" + FileName;
    PSBuffer.PSNumber = PSNumber;
    Client.PSData.push_back( PSBuffer );
    jCreate_PS_From_File(PSNumber++, const_cast<char*>(FileName.c_str()));
    }
    }
    }
    PSFile.close();
    Jim Langston, Sep 8, 2005
    #7
  8. Alex Vinokur Guest

    <> wrote in message news:...
    > i have a simple question guys
    >
    > i have to read a file that looks more or less like this
    >
    >
    > AND 1 2 3
    > OR 2 3 4
    > INV 5 2
    > BUF 7 1
    > AND 1 2
    >
    > INPUT 1 2 3 4 -1
    > OUTPUT 5 7 -1
    >
    >
    > (i just made up some random numbers but i guess that gives an idea of
    > the input file format)
    >
    > onw what i want to do is read this input into a structure (string name,
    > int array of inputs, int one output .. the value of these ints being
    > the values next to the logic gate)
    >
    > so for example
    >
    > name = AND, inputs[0] = 1, input[1] = 2, output = 3
    >
    >
    > my question is ... how do i parse this file ? (i can do the rest
    > myself)
    >

    [snip]

    Somehing like this.

    1. To read file into string
    http://groups.google.com/group/perfo/msg/530fae8e5e065030

    2. To split string into vector of vectors (let vv be name of the vector).
    http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0

    3. for (int i = 0; i < vv.size(); i++)
    {
    name = vv[0];
    for (int j = 1; j < vv.size() - 1; j++) input [j-1] = atoi(vv[j]);
    output = atoi(vv[vv.size() - 1]);
    }

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Sep 8, 2005
    #8
  9. Alex Vinokur Guest

    Alex Vinokur wrote:

    [snip]
    > 1. To read file into string
    > http://groups.google.com/group/perfo/msg/530fae8e5e065030
    >
    > 2. To split string into vector of vectors (let vv be name of the vector).
    > http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0

    [snip]

    Instead of two actions above you can read file to vector of vectors
    directly:
    http://groups.google.com/group/alt.sources/msg/5ba6823f110aa9cf

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Sep 9, 2005
    #9
    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. Darrel
    Replies:
    3
    Views:
    670
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    780
  3. Replies:
    1
    Views:
    451
    Keith Thompson
    Apr 12, 2005
  4. Lionel
    Replies:
    22
    Views:
    649
    Steve Holden
    Feb 3, 2009
  5. Robin Wenger
    Replies:
    191
    Views:
    3,230
Loading...

Share This Page