data format

G

Gary Wessle

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;
}
}
}
****************************************************************
 
J

John Harrison

Gary said:
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
 
G

Gary Wessle

John Harrison said:
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();
 
J

John Harrison

Gary said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top