473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3230

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
7
by: Timo Haberkern | last post by:
Hi there, i have some troubles with my TSearch2 Installation. I have done this installation as described in http://www.sai.msu.su/~megera/oddmuse/index.cgi/Tsearch_V2_compound_words...
2
by: fabrice | last post by:
hello, I nedd help to create and use Library Satellite. I'm using Web Form under framework 1.1 and VB.NET I'm not using Visual Studio. I'd like to internationalize my web application with...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
9
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.