File Reading

Discussion in 'C++' started by cplusplusquestion@gmail.com, Jul 7, 2008.

  1. Guest

    I have a binary file like:

    a bla bla bla bla
    b 12 23 34 80 38

    a bla bla bla bla
    b 23 45 89 30 99

    When I find first character of the line is 'a', I would like to skip
    reading this line, then go to next line. Any suggestion?
    , Jul 7, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I have a binary file like:
    >
    > a bla bla bla bla
    > b 12 23 34 80 38
    >
    > a bla bla bla bla
    > b 23 45 89 30 99
    >
    > When I find first character of the line is 'a', I would like to skip
    > reading this line, then go to next line. Any suggestion?


    If it's a binary file, how do you know where the next line begins?

    --
    Ian Collins.
    Ian Collins, Jul 7, 2008
    #2
    1. Advertising

  3. Brad Guest

    Ian Collins wrote:

    > If it's a binary file, how do you know where the next line begins?


    You don't... at least not 100% :)
    Brad, Jul 8, 2008
    #3
  4. Fred Guest

    On Jul 7, 3:48 pm, Ian Collins <> wrote:
    > wrote:
    > > I have a binary file like:

    >
    > > a bla bla bla bla
    > > b 12 23 34 80 38

    >
    > > a bla bla bla bla
    > > b 23 45 89 30 99

    >
    > > When I find first character of the line is 'a', I would like to skip
    > > reading this line, then go to next line.  Any suggestion?

    >
    > If it's a binary file, how do you know where the next line begins?
    >


    I would ask the OP the same question for a text file.
    --
    Fred Kleinschmidt
    Fred, Jul 8, 2008
    #4
  5. Eric Pruneau Guest

    "Ian Collins" <> a écrit dans le message de news:
    ...
    > wrote:
    >> I have a binary file like:
    >>
    >> a bla bla bla bla
    >> b 12 23 34 80 38
    >>
    >> a bla bla bla bla
    >> b 23 45 89 30 99
    >>
    >> When I find first character of the line is 'a', I would like to skip
    >> reading this line, then go to next line. Any suggestion?

    >
    > If it's a binary file, how do you know where the next line begins?
    >
    > --
    > Ian Collins.


    From what I see, you can do:

    1. read a char until you get a number
    2.read a char until you get a letter

    do this steps util the end of file

    here a way to do this (with little error checking), you can test it with a
    bin file like this one
    a bla bla bla blab 12 23 34 80 38a bla bla bla blab 23 45 89 30 99

    it can be on a single line, it doesn't matter.



    bool isNumeric(char tmp)
    {
    return tmp >= 48 && tmp <= 57; // from ascii table
    }

    bool isLetter(char tmp)
    {
    // from ascii table
    return (tmp >= 65 && tmp <= 90) || // A-Z
    (tmp >= 97 && tmp <= 122); // a-z
    }

    int main()
    {
    std::ifstream ifs("FileName.bin", ios_base::in | ios_base::binary);

    string linea , lineb;
    while(ifs)
    {
    // now read a line and b line
    char tmp=0;

    while(!isNumeric(tmp))
    {
    ifs.get(tmp);
    if(ifs)
    linea.push_back(tmp);
    else
    break;
    }
    int bpos = linea.rfind('b');
    lineb.assign(linea,bpos,linea.size()-bpos);
    linea.erase(linea.begin()+bpos, linea.end());
    // now you do your stuff with linea

    while(!isLetter(tmp))
    {
    ifs.get(tmp);
    if(ifs)
    lineb.push_back(tmp);
    else
    break;
    }
    // ok I assume the data in linea has been extracted and stored elsewhere
    so I can clear the string
    linea.clear();
    size_t apos = lineb.rfind('a');
    if(ifs)
    {
    linea.push_back('a');
    lineb.erase(lineb.begin()+apos, lineb.end());
    }
    // now extract info from lineb ....
    lineb.clear();
    }

    return 0;
    }


    --------------------

    Eric Pruneau
    Eric Pruneau, Jul 9, 2008
    #5
    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:
    669
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,404
    Scott Allen
    Jun 4, 2005
  3. Replies:
    0
    Views:
    778
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    366
    Hrvoje Niksic
    May 25, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    786
    Alf P. Steinbach
    Jul 10, 2007
Loading...

Share This Page