views:

264

answers:

5

Hello, I did it before... But I forgot. I have a file with some data:

0.5 0.6 0.7 1.2 1.5

How can I read this in c++? I did it with stream... something like:

float var = 0;
stream >> var;
A: 

Something like this. The << operator treats spaces as a separator.

float array[5] = {0.0f};

for(int i = 0; i < 5; i++)
{
    stream >> array[i];
}

BTW I did 5 since you had 5 in your example. (and I am assuming you have the stream setup)

Ólafur Waage
@Ólafur - how is that going to work? Perhaps my C++ syntax memory is waay off, but as I read it, you're going to read from the file into the *address* of `var`, not into the next position in the array. You should want to put the `[i]` back at the end of `var`, as I had modified for you earlier - unless this is a way of using arrays I've just never seen or heard of before.
warren
Ya just an error from me.
Ólafur Waage
A: 

Do you mean how to open a file and read data from it?

That should look something like this:

float var;
ifstream infile("filename");
if(infile.good()){
    while(!infile.eof()){
        infile >> var;
        cout << var << "is the next value\n";
    }
}
warren
why the downvote? is there something missing in my example?
warren
this is how I've always done file I/O; and how I've always seen it in various reference materials.
warren
warren, your code is incorrect, see my comment at Vijay's answer (I didn't downvote, though).
avakar
eof doesn't return true until after the first failed attempt to read from the file. So (unless the file ends immediately after the last number, without a newline) your loop will perform an extra iteration.
Thomas Padron-McCarthy
Actually, avakar's "bumped into the end of file during the last extraction" is a better explanation than my "first failed attempt to read". If the file ends immediately after the last number, eof will turn true after the last ">>" operation that was successfull. Yes, on a lower level there was a byte-read that failed involved, but the ">>" operation was successful in that case, so "bumped into" is much better pedagogically.
Thomas Padron-McCarthy
@avakar - don't see an answer from Vijay. @Thomas - that's what the `.good()` call is supposed to do, I thought - make sure the file is healthy before continuing? I don't mind being wrong, but I'm a little concerned that this is how every reference manual and teacher I've ever seen has done it, and how I've been doing it for ~15 years, and it's not right :-\
warren
The other answer was deleted. The call to good just checks that the file could be opened, and doesn't help for the problem with the loop. And yes, this is a horribly common error. (So don't delete your answer, you too! It, and the discussion, is needed as a warning.)
Thomas Padron-McCarthy
+3  A: 

Something like this?

std::ifstream stream("C:/a.txt");
    float var = 0;
    while(stream >> var)
    {
     //Do some processing
    }
Naveen
`eof()` returns true only after the extraction fails.
avakar
I have edited the answer to make it safe. @Ockonal: Please check the edited answer.
Naveen
Okay, thanks. I've chaged that.
Ockonal
A: 

To read from files, use std::ifstream.

Extrakun
+3  A: 

The following snippet should give you a clue. Don't forget to include <fstream>.

std::ifstream fin("filename.txt");
float value;
while (fin >> value)
{
    // Do whatever you want with the value
}

Do not try to test fin.eof() it won't tell you if you're about to bump to the end of file.

avakar