Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading files

Reply
Thread Tools

reading files

 
 
Harry Overs
Guest
Posts: n/a
 
      08-26-2004
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,


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      08-26-2004
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

 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      08-26-2004
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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-26-2004

"Harry Overs" <(E-Mail Removed)> wrote in message
news:cgkpdt$2au$(E-Mail Removed)...
> 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


 
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
Reading files inside zip files from Javascript Narendra Sisodiya Javascript 7 12-08-2009 02:15 AM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
JAR files reading list files rob hadow Java 4 05-21-2004 04:41 PM



Advertisments