problem in parsing double prec. numbers

Discussion in 'C++' started by Tomasz Bednarz, Jul 29, 2006.

  1. Can someone help me to parse double precision numbers from text file?

    I have a sample text file which is as follows:

    VARIABLES = x, y
    ZONE I=11, J=11
    1.10000000000 0.10000000000
    0.10000000000 0.20000000000
    0.20000000000 0.30000000000
    0.30000000000 0.40000000000
    0.40000000000 0.50000000000
    ....
    ....

    The problem is in reading x and y...
    I did such function:

    istringstream stm;
    string lline;
    ifstream fp (filename);
    if ( fp.is_open() )
    {
    for (i...
    for (j...
    {
    getline(fp,lline);
    stm.str(lline);
    stm >> x(i,j) >> y(i,j);
    }
    ....
    ....

    It works only on some files (when using g++) don't know why :( and doesn't
    work at all when using compiler from visual c++ 2005!
    Am getting nothing in x(i,j) and y(i,j) (zeros)...

    Please give me some suggestions if you could,

    kind regards,
    Tomasz
     
    Tomasz Bednarz, Jul 29, 2006
    #1
    1. Advertising

  2. Tomasz Bednarz

    Moonlit Guest

    Hi,

    Don't know if this is the problem but for one thing files on windows are
    opened as text files while on unix'es there is no difference so it is better
    to open the files as binary (so the result is the same in vs as it is in
    g++)

    ifstream Input( Filename.c_tr(), ios_base::binary);

    Another thing is I think it is better to account for more numbers on a line

    while( strm >> number1 >> number2 )
    {
    ....
    }

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "Tomasz Bednarz" <> wrote in message
    news:eafb4i$dgh$...
    >
    > Can someone help me to parse double precision numbers from text file?
    >
    > I have a sample text file which is as follows:
    >
    > VARIABLES = x, y
    > ZONE I=11, J=11
    > 1.10000000000 0.10000000000
    > 0.10000000000 0.20000000000
    > 0.20000000000 0.30000000000
    > 0.30000000000 0.40000000000
    > 0.40000000000 0.50000000000
    > ...
    > ...
    >
    > The problem is in reading x and y...
    > I did such function:
    >
    > istringstream stm;
    > string lline;
    > ifstream fp (filename);
    > if ( fp.is_open() )
    > {
    > for (i...
    > for (j...
    > {
    > getline(fp,lline);
    > stm.str(lline);
    > stm >> x(i,j) >> y(i,j);
    > }
    > ...
    > ...
    >
    > It works only on some files (when using g++) don't know why :( and
    > doesn't work at all when using compiler from visual c++ 2005!
    > Am getting nothing in x(i,j) and y(i,j) (zeros)...
    >
    > Please give me some suggestions if you could,
    >
    > kind regards,
    > Tomasz
    >
    >
     
    Moonlit, Jul 29, 2006
    #2
    1. Advertising

  3. Tomasz Bednarz

    Moonlit Guest

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "Moonlit" <news moonlit xs4all nl> wrote in message
    news:44cb48d3$0$4531$4all.nl...
    > Hi,
    >
    > Don't know if this is the problem but for one thing files on windows are
    > opened as text files while on unix'es there is no difference so it is
    > better to open the files as binary (so the result is the same in vs as it
    > is in g++)
    >
    > ifstream Input( Filename.c_tr(), ios_base::binary);

    Sorry that should be Filename.c_str() obviously :)
    >
    > Another thing is I think it is better to account for more numbers on a
    > line
    >
    > while( strm >> number1 >> number2 )
    > {
    > ...
    > }
    >
    > --
    >
    >
    > Regards, Ron AF Greve
    >
    > http://moonlit.xs4all.nl
    >
    > "Tomasz Bednarz" <> wrote in message
    > news:eafb4i$dgh$...
    >>
    >> Can someone help me to parse double precision numbers from text file?
    >>
    >> I have a sample text file which is as follows:
    >>
    >> VARIABLES = x, y
    >> ZONE I=11, J=11
    >> 1.10000000000 0.10000000000
    >> 0.10000000000 0.20000000000
    >> 0.20000000000 0.30000000000
    >> 0.30000000000 0.40000000000
    >> 0.40000000000 0.50000000000
    >> ...
    >> ...
    >>
    >> The problem is in reading x and y...
    >> I did such function:
    >>
    >> istringstream stm;
    >> string lline;
    >> ifstream fp (filename);
    >> if ( fp.is_open() )
    >> {
    >> for (i...
    >> for (j...
    >> {
    >> getline(fp,lline);
    >> stm.str(lline);
    >> stm >> x(i,j) >> y(i,j);
    >> }
    >> ...
    >> ...
    >>
    >> It works only on some files (when using g++) don't know why :( and
    >> doesn't work at all when using compiler from visual c++ 2005!
    >> Am getting nothing in x(i,j) and y(i,j) (zeros)...
    >>
    >> Please give me some suggestions if you could,
    >>
    >> kind regards,
    >> Tomasz
    >>
    >>

    >
    >
     
    Moonlit, Jul 29, 2006
    #3
  4. Tomasz Bednarz

    Ron Natalie Guest

    Tomasz Bednarz wrote:

    > It works only on some files (when using g++) don't know why :( and doesn't
    > work at all when using compiler from visual c++ 2005!
    > Am getting nothing in x(i,j) and y(i,j) (zeros)...
    >
    > Please give me some suggestions if you could,
    >

    You might try explicitly clearing the flags on the stream (or
    just move the definition inside the for loops so that you get
    a new one each time.

    The standard doesn't explicitly say that str() clears the
    flag when "initializing the sequences". If you get an error
    (as you will on the first two lines that don't have numeric
    values) it will never succeed later.
     
    Ron Natalie, Jul 29, 2006
    #4
    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. GIMME
    Replies:
    2
    Views:
    877
    GIMME
    Feb 11, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  3. Subra
    Replies:
    25
    Views:
    1,205
    user923005
    Mar 8, 2007
  4. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    571
    Howard
    May 27, 2007
  5. Lance Hoffmeyer
    Replies:
    2
    Views:
    529
    Lance Hoffmeyer
    Jul 26, 2007
Loading...

Share This Page