469,326 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Problems reading strings from files

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.

Jun 11 '06 #1
4 3025

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@m38g2000cwc.googlegr oups.com...
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 4 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?) 4 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.
--
Sumit Rajan <su*********@gmail.com>
Jun 11 '06 #2
Gaijinco wrote:
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.

Thanks.


Ben
Jun 11 '06 #3

Gaijinco wrote:
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 <fe****@gmail.com>

Jun 11 '06 #4
I can't believe how stupid I am!

Thanks a lot for pointing the obvious!

If there's someone intrested, this is what it was about:

http://acm.uva.es/p/v101/10191.html

Thanks again!

Jun 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by nightflyer | last post: by
24 posts views Thread by Hendrik Schober | last post: by
7 posts views Thread by John Dann | last post: by
2 posts views Thread by fabrice | last post: by
18 posts views Thread by John | last post: by
9 posts views Thread by Sheldon | last post: by
10 posts views Thread by Tyler | last post: by
6 posts views Thread by efrenba | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.