reading files

H

Harry Overs

I'm using the ifstream to read the contents of a file using the >>
operators.

However the problem that I have is that the file is made up of double and
string variables. Is there an easy way to read both types in or should I
just read it all in as char* and then convert to the correct format?

cheers,
 
T

Thomas Matthews

Harry said:
I'm using the ifstream to read the contents of a file using the >>
operators.

However the problem that I have is that the file is made up of double and
string variables. Is there an easy way to read both types in or should I
just read it all in as char* and then convert to the correct format?

cheers,
If you know when to read in the doubles, then read in using
strings and doubles.

Although you may find that using std::string will be easier
than C style strings.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jacek Dziedzic

Harry said:
I'm using the ifstream to read the contents of a file using the >>
operators.

However the problem that I have is that the file is made up of double and
string variables. Is there an easy way to read both types in or should I
just read it all in as char* and then convert to the correct format?

If you know which fields are double and which are string, than yes,
you could use the ">>" operator to easily read them:

myfile >> mydouble >> mydouble >> mystring >> ...

The trouble with reading strings via ">>" is, however, that
if you have spaces embedded in your strings they will split the
input by words. In that case you might want to look at the
get(...) and getline(...) functions.

HTH,
- J.
 
M

Mike Wahler

Harry Overs said:
I'm using the ifstream to read the contents of a file using the >>
operators.

However the problem that I have is that the file is made up of double and
string variables. Is there an easy way to read both types in

Yes, but you'll need to know in advance the exact format of the
data, and depend upon it being correct or make sure bad data is
handled properly.

std::ifstream in("filename");
double d;
std::string s;

in >> d >> s; // read a double, then a string

But if the characters making up the 'double' value are invalid,
'in >> d' will fail, leaving those characters in the stream,
and setting the stream to a 'fail' state, in which it will stay
until you specifically reset it. Another problem is that if
any of your strings contain whitespace, 'in >> s' will stop
reading at the first one. E.g input of "John Doe" will cause
extraction of only "John".
or should I
just read it all in as char* and then convert to the correct format?

Almost. Change 'char*' to 'std::string', and I agree.


IMO this is the best method to 'default' to. You get better control.
A function such as 'strtod()' can do the numeric conversions and has good
error reporting.

while stream state == good
Read a line.
Convert (if numeric) and store.
go to 1

if stream state != eof
read error

-Mike
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top