problem in parsing double prec. numbers

T

Tomasz Bednarz

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
 
M

Moonlit

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
 
M

Moonlit

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

Moonlit said:
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 :)
 
R

Ron Natalie

Tomasz said:
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.
 

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

Similar Threads

Crossword 2
Rearranging .ply file via C++ String Parsing 0
std::map<> problem 3
Crossword 14
TF-IDF 1
RNGs: A double KISS 10
adding array of numbers 5
Mapping of node numbers 0

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top