Good file I/O?
Sep 8, 2010 at 9:22pm UTC
Hey guys. I have a text file in the general format of:
1 2 3 4 5 6 7
L_x = 5.0e-6
L_y = 1.25e-6
L_z = 1.25e-6
npart = 1000000
nstep = 10000
ncell_x = 100
x_vel_offset = 500
And more. Basically, I want to read these numbers in. How can I make it read the number after the equals sign for each line? Or is there a better way to do this? Would I have to use regex?
Thanks!
Sep 8, 2010 at 9:26pm UTC
Does this work?
1 2 3 4 5 6 7
ifstream f( "file" );
string name, delim;
double n;
while ( f >> name >> delim >> n ) {
assert( delim == "=" );
// do something with n
}
A better way might be to use getline to read a line at a time and parse it out with a stringstream.
Last edited on Sep 8, 2010 at 9:28pm UTC
Topic archived. No new replies allowed.