Problems reading strings from files

G

Gaijinco

I had a file named nap.in which looks like this:

4
10:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 17:45 Reading
4
10:00 12:00 Lectures
12:00 13:00 Lunch, just lunch.
13:00 15:00 Lectures, lectures... oh, no!
16:45 17:45 Reading (to be or not to be?)
4
10:00 12:00 Lectures, as everyday.
12:00 13:00 Lunch, again!!!
13:00 15:00 Reading (I love reading, but should I schedule it?)
1
12:00 13:00 I love lunch! Have you ever noticed it? :)

I'm using this to read the file and extract the data:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream in("nap.in");

string line;
while(getline(in,line))
{
int n = atoi(line.c_str());
for(int times=1; times<=n; ++times)
{
getline(in,line);
cout << line << endl;
}
}
return 0;
}

But it is outputing this:

10:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 17:45 Reading
10:00 12:00 Lectures
12:00 13:00 Lunch, just lunch.
13:00 15:00 Lectures, lectures... oh, no!
16:45 17:45 Reading (to be or not to be?)
10:00 12:00 Lectures, as everyday.
12:00 13:00 Lunch, again!!!
13:00 15:00 Reading (I love reading, but should I schedule it?)
1













The BIG whitespace is actually part of the output.

What is happening?

Thanks.
 
S

Sumit Rajan

Gaijinco said:
I had a file named nap.in which looks like this:

4

Here n is 4. So the next four lines are read(and displayed) without a
problem.
10:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 17:45 Reading
Same here.
10:00 12:00 Lectures
12:00 13:00 Lunch, just lunch.
13:00 15:00 Lectures, lectures... oh, no!
16:45 17:45 Reading (to be or not to be?)
Again n=4, so the next four lines will be read and displayed(including the
line that just reads 1; Are you sure that is what you want? Are you sure
that you haven't missed out a line before the one that reads "1"?).
10:00 12:00 Lectures, as everyday.
12:00 13:00 Lunch, again!!!
13:00 15:00 Reading (I love reading, but should I schedule it?)
1

12:00 13:00 I love lunch! Have you ever noticed it? :)

And here, the atof is likely to return 12 for the above line. Again, is that
what you want?
I'm using this to read the file and extract the data:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream in("nap.in");

string line;
while(getline(in,line))
{
int n = atoi(line.c_str());

Add this line here to get a better idea of what is happening:
cout << "n= " << n << endl;

for(int times=1; times<=n; ++times)
{
getline(in,line);

You may also consider changing the above line to:
if (getline(in,line))
cout << line << endl;
}

Regards,
Sumit.
 
B

benben

Gaijinco said:
I had a file named nap.in which looks like this:

4
10:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 17:45 Reading
4
10:00 12:00 Lectures
12:00 13:00 Lunch, just lunch.
13:00 15:00 Lectures, lectures... oh, no!
16:45 17:45 Reading (to be or not to be?)
4

Shouldn't this be 3?
10:00 12:00 Lectures, as everyday.
12:00 13:00 Lunch, again!!!
13:00 15:00 Reading (I love reading, but should I schedule it?)
1
12:00 13:00 I love lunch! Have you ever noticed it? :)

I'm using this to read the file and extract the data:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream in("nap.in");

string line;
while(getline(in,line))
{
int n = atoi(line.c_str());
for(int times=1; times<=n; ++times)
{
getline(in,line);
cout << line << endl;
}
}
return 0;
} [snip]
The BIG whitespace is actually part of the output.

What is happening?

When do people learn to debug?! AFAICS your problem is from your input
file. It can be easily caught from a simple debugging. So run your
debugger before you post it here.

Ben
 
L

Lucman

Gaijinco said:
I had a file named nap.in which looks like this:

4
Here the number 4 means 4 lines to be read, so therefore you have 4
lines of strings.
10:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 17:45 Reading
4
Here again,the number 4 means 4 lines to be read, so therefore you have
4 lines of strings.
10:00 12:00 Lectures
12:00 13:00 Lunch, just lunch.
13:00 15:00 Lectures, lectures... oh, no!
16:45 17:45 Reading (to be or not to be?)
4
Here again, the number 4 means 4 lines to be read, but note: you only
have 3 lines of strings below. therefore it includes the number 1 and
the other will be garbaged.
10:00 12:00 Lectures, as everyday.
12:00 13:00 Lunch, again!!!
13:00 15:00 Reading (I love reading, but should I schedule it?)
1
12:00 13:00 I love lunch! Have you ever noticed it? :)

Regards,
Lucman <[email protected]>
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top