By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

reading files

P: n/a
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,
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Harry Overs wrote:
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

Jul 22 '05 #2

P: n/a
Harry Overs wrote:
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.
Jul 22 '05 #3

P: n/a

"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cg**********@hamble.qinetiq.com...
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.