Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   data format (http://www.velocityreviews.com/forums/t482037-data-format.html)

Gary Wessle 03-05-2007 05:59 PM

data format
 
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::ofstream ofs( f.c_str() );
for (int i = 0; i < lines.size(); ++i)
ofs << lines[i] << 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;
}
}
}
************************************************** **************

John Harrison 03-05-2007 06:08 PM

Re: data format
 
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

Gary Wessle 03-05-2007 10:46 PM

Re: data format
 
John Harrison <john_andronicus@hotmail.com> 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::ofstream ofs( f.c_str() );
for (int i = 0; i < lines.size(); ++i)
ofs << lines[i] << std::endl;
ofs.close();

to

ifs.close();
std::ofstream ofs( f.c_str() );
for (int i = 0; i < lines.size(); ++i)
ofs << fixed << lines[i] << std::endl;
ofs.close();

John Harrison 03-05-2007 10:55 PM

Re: data format
 
Gary Wessle wrote:
> John Harrison <john_andronicus@hotmail.com> 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::ofstream ofs( f.c_str() );
> for (int i = 0; i < lines.size(); ++i)
> ofs << lines[i] << std::endl;
> ofs.close();
>
> to
>
> ifs.close();
> std::ofstream ofs( f.c_str() );
> for (int i = 0; i < lines.size(); ++i)
> ofs << fixed << lines[i] << 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


All times are GMT. The time now is 11:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.