data format

Discussion in 'C++' started by Gary Wessle, Mar 5, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I wrote 2 functions "included at the bottom" one inserts and one
    reads data from a file, the data format is as here

    ****************
    a 2
    b -0.0336974
    _time 1.17312e+09
    ****************

    when I use the routine to read the value "_time", it reads as
    1.17312e+09 but when it was entered it was 1173116800 and I want it
    read back in the same correct format 1173106800.

    how can I solve this with out screwing up the other data for a and b?

    thanks


    ****************************************************************
    // Set the value for the loopup string from file
    void Orde_task::status(std::string lookup, double new_val)
    {
    std::string f = p_info->pair_status_dir + fxp;
    std::ifstream ifs( f.c_str() );
    std::vector<std::string> lines;
    std::string line, word;
    double val;
    while( getline( ifs, line ) && line != "" )
    {
    std::stringstream ss( line) ;
    ss >> word >> val;
    if( word == lookup )
    val = new_val;
    std::stringstream sss;
    sss << word << " " << val;
    std::string tmp = sss.str();
    lines.push_back( tmp );
    }
    ifs.close();
    std::eek:fstream ofs( f.c_str() );
    for (int i = 0; i < lines.size(); ++i)
    ofs << lines << std::endl;
    ofs.close();
    }


    // Get the value for the lookup string from file.
    double Orde_task::status( std::string lookup)
    {
    std::string f = p_info->pair_status_dir + fxp;
    std::ifstream ifs( f.c_str() );
    std::string line, word;
    double val;
    while( getline( ifs, line ) && line != "" )
    {
    std::stringstream ss( line );
    ss >> word >> val;
    if( word == lookup )
    {
    ifs.close();
    return val;
    }
    }
    }
    ****************************************************************
    Gary Wessle, Mar 5, 2007
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > Hi
    >
    > I wrote 2 functions "included at the bottom" one inserts and one
    > reads data from a file, the data format is as here
    >
    > ****************
    > a 2
    > b -0.0336974
    > _time 1.17312e+09
    > ****************
    >
    > when I use the routine to read the value "_time", it reads as
    > 1.17312e+09 but when it was entered it was 1173116800 and I want it
    > read back in the same correct format 1173106800.
    >
    > how can I solve this with out screwing up the other data for a and b?
    >
    > thanks
    >


    You've got this the wrong way round. In your program there are just
    numbers (no formats), its only when you *write* out to a file that a
    format is chosen.

    Looking at your code (but not running it) it looks to me like you need
    to change

    std::stringstream sss;
    sss << word << " " << val;
    std::string tmp = sss.str();
    lines.push_back( tmp );

    to

    std::stringstream sss;
    sss << fixed << word << " " << val;
    std::string tmp = sss.str();
    lines.push_back( tmp );

    fixed will prevent C++ from using the 'scientific' format you don't like.

    john
    John Harrison, Mar 5, 2007
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    John Harrison <> writes:

    > Gary Wessle wrote:
    > > Hi
    > > I wrote 2 functions "included at the bottom" one inserts and one
    > > reads data from a file, the data format is as here
    > > ****************
    > > a 2
    > > b -0.0336974
    > > _time 1.17312e+09
    > > ****************
    > > when I use the routine to read the value "_time", it reads as
    > > 1.17312e+09 but when it was entered it was 1173116800 and I want it
    > > read back in the same correct format 1173106800.
    > > how can I solve this with out screwing up the other data for a and b?
    > > thanks
    > >

    >
    > You've got this the wrong way round. In your program there are just
    > numbers (no formats), its only when you *write* out to a file that a
    > format is chosen.
    >
    > Looking at your code (but not running it) it looks to me like you need
    > to change
    >
    > std::stringstream sss;
    > sss << word << " " << val;
    > std::string tmp = sss.str();
    > lines.push_back( tmp );
    >
    > to
    >
    > std::stringstream sss;
    > sss << fixed << word << " " << val;
    > std::string tmp = sss.str();
    > lines.push_back( tmp );
    >
    > fixed will prevent C++ from using the 'scientific' format you don't like.
    >
    > john


    since the part of the code that prints out to the file is not what you
    stated, did you mean?

    ifs.close();
    std::eek:fstream ofs( f.c_str() );
    for (int i = 0; i < lines.size(); ++i)
    ofs << lines << std::endl;
    ofs.close();

    to

    ifs.close();
    std::eek:fstream ofs( f.c_str() );
    for (int i = 0; i < lines.size(); ++i)
    ofs << fixed << lines << std::endl;
    ofs.close();
    Gary Wessle, Mar 5, 2007
    #3
  4. Gary Wessle wrote:
    > John Harrison <> writes:
    >
    >
    >>Gary Wessle wrote:
    >>
    >>>Hi
    >>>I wrote 2 functions "included at the bottom" one inserts and one
    >>>reads data from a file, the data format is as here
    >>>****************
    >>>a 2
    >>>b -0.0336974
    >>>_time 1.17312e+09
    >>>****************
    >>>when I use the routine to read the value "_time", it reads as
    >>>1.17312e+09 but when it was entered it was 1173116800 and I want it
    >>>read back in the same correct format 1173106800.
    >>>how can I solve this with out screwing up the other data for a and b?
    >>>thanks
    >>>

    >>
    >>You've got this the wrong way round. In your program there are just
    >>numbers (no formats), its only when you *write* out to a file that a
    >>format is chosen.
    >>
    >>Looking at your code (but not running it) it looks to me like you need
    >>to change
    >>
    >> std::stringstream sss;
    >> sss << word << " " << val;
    >> std::string tmp = sss.str();
    >> lines.push_back( tmp );
    >>
    >>to
    >>
    >> std::stringstream sss;
    >> sss << fixed << word << " " << val;
    >> std::string tmp = sss.str();
    >> lines.push_back( tmp );
    >>
    >>fixed will prevent C++ from using the 'scientific' format you don't like.
    >>
    >>john

    >
    >
    > since the part of the code that prints out to the file is not what you
    > stated, did you mean?
    >
    > ifs.close();
    > std::eek:fstream ofs( f.c_str() );
    > for (int i = 0; i < lines.size(); ++i)
    > ofs << lines << std::endl;
    > ofs.close();
    >
    > to
    >
    > ifs.close();
    > std::eek:fstream ofs( f.c_str() );
    > for (int i = 0; i < lines.size(); ++i)
    > ofs << fixed << lines << std::endl;
    > ofs.close();



    No I meant what I said. Your code (is it your code?) has a two stage
    process to write out the data, first the data is written to a vector of
    strings (the lines variable), then the vector of strings is written out
    to a file. It when the numbers are written to the string vector that the
    format is selected.

    john
    John Harrison, Mar 5, 2007
    #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. Ram
    Replies:
    1
    Views:
    564
    Mike Treseler
    Feb 24, 2005
  2. Brian Candy
    Replies:
    2
    Views:
    1,137
    Janaka
    Feb 18, 2004
  3. Andy
    Replies:
    7
    Views:
    6,236
    Roedy Green
    May 10, 2004
  4. Girish
    Replies:
    0
    Views:
    387
    Girish
    Feb 13, 2004
  5. Elijah Odumosu
    Replies:
    1
    Views:
    178
    Roger Pack
    Apr 29, 2008
Loading...

Share This Page