Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > data format

Reply
Thread Tools

data format

 
 
Gary Wessle
Guest
Posts: n/a
 
      03-05-2007
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:fstream 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;
}
}
}
************************************************** **************
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      03-05-2007
John Harrison <(E-Mail Removed)> 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:fstream ofs( f.c_str() );
for (int i = 0; i < lines.size(); ++i)
ofs << lines[i] << std::endl;
ofs.close();

to

ifs.close();
std:fstream ofs( f.c_str() );
for (int i = 0; i < lines.size(); ++i)
ofs << fixed << lines[i] << std::endl;
ofs.close();
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-05-2007
Gary Wessle wrote:
> John Harrison <(E-Mail Removed)> 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:fstream ofs( f.c_str() );
> for (int i = 0; i < lines.size(); ++i)
> ofs << lines[i] << std::endl;
> ofs.close();
>
> to
>
> ifs.close();
> std:fstream 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting HTML input data to pre-format XML data Elijah Odumosu Ruby 1 04-29-2008 06:56 PM
simple programs to deal with data format, data synchronisation Ram VHDL 1 02-24-2005 05:34 PM
NTFS quick format and normal format Guan Foo Wah MCSE 2 05-08-2004 11:35 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
(XML) HTTP(S) in the Auto-lead Data Format (ADF) 1.0. HOW DO I READ THE DATA? Girish ASP General 0 02-13-2004 12:46 AM



Advertisments