473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

reading files

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
3 2458
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.