PDA

View Full Version : Reading files in C++



maverick_pol
14th May 2008, 17:37
Hi guys,

I have a file containing lines like this one:

[double]_[double]_[double]_[int]_[int]\n

_ -> space. There are 5 values separated by space. I have already written a code in C that reads these kind of files. Now I need the same functionality using C++ code.
How to read line of a file this kind?
I have some ideas, but I haven't written much code in C++ managing files that's why I am asking for advice.

Also I have a struct(5 values the same types as above) and they need to be store the same way.

thanks.

Kacper

jpn
14th May 2008, 18:14
Use ifstream:


#include <fstream>

struct {
double a;
double b;
double c;
int d;
int e;
} var;

std::ifstream input("numbers.txt");
input >> var.a >> var.b >> var.c >> var.d >> var.e;

maverick_pol
15th May 2008, 08:53
Hi,

No comment, couldn't be easier.

THanks.

Kacper