Reading a file that looks like this..

E

Eric Lilja

Hello, I have a text file with following format:
number one_word
string
number number char string

There may be several lines of the last type (number number char
string), the number of such lines should correspond to the first number
in the file. Anyway, how do I read such a file if I need the numbers
stored in ints, the single char stored in a char and the strings (words
or sentences) stored in a std::string? I tried:

static void
read_and_display_file(ifstream& file)
{
int num = 0;
string str;

/* We know the first line is a number and a single word. */
file >> num >> str;

cout << num << " " << str << endl;

/* We know the second line is a string (that contain spaces). */
getline(file, str);

cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

/* Now we loop the number of times as stated at the beginning of
the file. The line all have the following format:
int int char some_string_that_may_contain_spaces */
for(int i = 0; i < num; ++i)
{
/* First we read the two ints and the char. */
file >> a >> b >> c;

/* The we read the remainder of the line into a std::string. */
getline(file, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}

My testfile:
4 word
a sentence
1 2 a dld dld
3 4 b dlsl dlfefef
5 6 c slspod sejdjd
7 8 d lsll spd dslddd


The output when I run my testprogram:
$ ./foo.exe
4 word

0 0
0 0
0 0
0 0

Please help me solve this, thanks!
 
E

Eric Lilja

Eric said:
Hello, I have a text file with following format:
number one_word
string
number number char string

There may be several lines of the last type (number number char
string), the number of such lines should correspond to the first number
in the file. Anyway, how do I read such a file if I need the numbers
stored in ints, the single char stored in a char and the strings (words
or sentences) stored in a std::string? I tried:

static void
read_and_display_file(ifstream& file)
{
int num = 0;
string str;

/* We know the first line is a number and a single word. */
file >> num >> str;

Ok, here's the error. I need to call getline() here to skip to the
second line
then I can call it again to actually read the contents of the second
line.
Determined by printing what file.peek() returned here.
cout << num << " " << str << endl;

/* We know the second line is a string (that contain spaces). */
getline(file, str);

cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

/* Now we loop the number of times as stated at the beginning of
the file. The line all have the following format:
int int char some_string_that_may_contain_spaces */
for(int i = 0; i < num; ++i)
{
/* First we read the two ints and the char. */
file >> a >> b >> c;

/* The we read the remainder of the line into a std::string. */
getline(file, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}

My testfile:
4 word
a sentence
1 2 a dld dld
3 4 b dlsl dlfefef
5 6 c slspod sejdjd
7 8 d lsll spd dslddd


The output when I run my testprogram:
$ ./foo.exe
4 word

0 0
0 0
0 0
0 0

Please help me solve this, thanks!

/ Eric
 
I

int2str

Eric said:
[snipped code]

I think you shouldn't mix getline and extractors too much like you do.
The state of the stream gets very confusing to manage if you do this.
Also you're missing a lot of error handling.

Further, I wouldn't rely on the value you read from the file as a for
index parameter. Instead read until the end of the file.

Here's how I would rewrite your function:

static void read_and_display_file(ifstream& file)
{
string line;
stringstream ss;

int num = 0;
string str;

getline( file, line );
ss.str( line );

// We know the first line is a number and a single word.

ss >> num >> str;
cout << num << " " << str << endl;

// We know the second line is a string (that contain spaces).
getline(file, str);
cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

// Now we loop the number of times as stated at the beginning of
// the file. The line all have the following format:
// int int char some_string_that_may_contain_spaces
while( getline( file, line ))
{
ss.clear();
ss.str(line);

// First we read the two ints and the char.
ss >> a >> b >> c;

// The we read the remainder of the line into a std::string.
getline(ss, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}


Cheers,
Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top