473,383 Members | 1,874 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,383 software developers and data experts.

NEWLINE IN IOSTREAM


I've got some trick input that I need help reading.

I am using iostream because it is still a holiday weekend and I'm not
trying to stress, but I have some input that looks like this.

2

1453
2123
3342
4234
5123
6511
6123

1234
1234
2345
3456
3456
7432

Where the first number indicates a number of cases followed by an unknown
number of lines, for n case seperated be a empty line.

My question is how can I use iostream to tell the diffrence between a read
on an empty line and the other. I'm not sure that this makes sense,
because by default iostream ignores white space.

--Chad
Nov 27 '05 #1
3 4598

"Chad E. Dollins" <vi****@cs.utexas.edu> wrote in message
news:Pi*******************************@galley.cs.u texas.edu...

I've got some trick input that I need help reading.

I am using iostream because it is still a holiday weekend and I'm not
trying to stress, but I have some input that looks like this.

2

1453
2123
3342
4234
5123
6511
6123

1234
1234
2345
3456
3456
7432

Where the first number indicates a number of cases followed by an unknown
number of lines, for n case seperated be a empty line.

My question is how can I use iostream to tell the diffrence between a read
on an empty line and the other. I'm not sure that this makes sense,
because by default iostream ignores white space.


It's not an istream object that ignores whitespace, but
the overloaded >> operator. You can read an entire line
at a time with the std::getline function (declared by <string>).

-Mike
Nov 27 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:X0*****************@newsread1.news.pas.earthl ink.net...

"Chad E. Dollins" <vi****@cs.utexas.edu> wrote in message
news:Pi*******************************@galley.cs.u texas.edu...

I've got some trick input that I need help reading.

I am using iostream because it is still a holiday weekend and I'm not
trying to stress, but I have some input that looks like this.

2

1453
2123
3342
4234
5123
6511
6123

1234
1234
2345
3456
3456
7432

Where the first number indicates a number of cases followed by an unknown
number of lines, for n case seperated be a empty line.

My question is how can I use iostream to tell the diffrence between a
read on an empty line and the other. I'm not sure that this makes sense,
because by default iostream ignores white space.


It's not an istream object that ignores whitespace, but
the overloaded >> operator. You can read an entire line
at a time with the std::getline function (declared by <string>).

-Mike


#include <fstream>
#include <istream>
#include <iostream>
#include <sstream>
#include <string>

std::istream& parse(std::istream& in)
{
std::string line;
while(in)
{
while(in && line.empty())
{
std::getline(in, line);
}

std::istringstream iss(line);
unsigned int count(0);
iss >> count;

do
{
std::getline(in, line);
} while(in && line.empty());

std::cout << "count == " << count << '\n';

for(unsigned int i(0); i < count; ++i)
{
while(in && !line.empty())
{
std::cout << line << '\n';
std::getline(in, line);
}

std::cout << "[End of group " << i + 1 << "]\n";

while(in && line.empty())
std::getline(in, line);
}
}

if(in.eof())
std::cout << "<End of Input>\n";
else
std::cout << "** Error reading input**\n";

return in;
}

int main()
{
std::ifstream input("C:/test.txt");

if(input)
parse(input);

return 0;
}
/* ** (Not thoroughly tested ** */

Input:
2

1453
2123
3342
4234
5123
6511
6123

1234
1234
2345
3456
3456
7432

3

1453
2123
3342
4234
5123
6511
6123
1234
1234
2345
3456
3456
7432


1234
1234
2345
3456
3456
7432

Output:

count == 2
1453
2123
3342
4234
5123
6511
6123
[End of group 1]
1234
1234
2345
3456
3456
7432
[End of group 2]
count == 3
1453
2123
3342
4234
5123
6511
6123
[End of group 1]
1234
1234
2345
3456
3456
7432
[End of group 2]
1234
1234
2345
3456
3456
7432
[End of group 3]
<End of Input>

-Mike
Nov 27 '05 #3
Chad E. Dollins wrote:
My question is how can I use iostream to tell the diffrence between a read
on an empty line and the other.


You simple need to process the whitespaces yourself rather than
leaving it to the formatted extractor. To do so, you need to
understand that formatted extractors begin processing by skipping
whitespaces. That is, after reading anything, the following
whitespaces are not touched and you can simple read them yourself.

Here is a possible approach how to do this conveniently:

#include <iostream>
#include <ctype.h>
#include <stdexcept>

struct nlcount { int count; nlcount(): count(0) {} };

std::istream& operator>> (std::istream& in, nlcount& nls)
{
std::istream::sentry kerberos(in, true);
if (kerberos)
{
std::streambuf* sbuf = in.rdbuf();
int eof = std::char_traits<char>::eof();
nls.count = 0;
int c = sbuf->sgetc();
for (; c != eof && std::isspace(c); c = sbuf->snextc())
if (c == std::char_traits<char>::to_int_type('\n'))
++nls.count;
if (c == eof)
in.setstate(std::ios_base::eofbit);
}
return in;
}

void read(std::istream& in)
{
int blocks = 0;
nlcount nls;
if (!(in >> blocks >> nls) || nls.count != 2)
throw std::runtime_error("missing newline after block count");

int value = 0;
for (int count = 1; count <= blocks && in; )
if (in >> value >> nls)
{
std::cout << count << ": " << value << "\n";
if (nls.count == 2)
++count;
}
}

int main()
{
try { read(std::cin); }
catch (std::exception const& ex) {
std::cerr << "ERROR: " << ex.what() << "\n";
}
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Nov 27 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
7
by: Alex Nordhus | last post by:
I am looking for a way to strip the blank line and the empty newline at the end of the text file. I can get the blank lines removed from the file but it always leaves the end line (which is blank)...
8
by: surendran.d | last post by:
hi, I am using gcc 3.4.4 for c++, i am getting the following warning if there is no new line at the EOF. Why this warning and i am not getting the warning in vc++ compiler. thanks, suri
4
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
3
by: dcwbxb | last post by:
Trying to create a program which reads and echos the contents of an input data file, which consists of two records. When I complile my code I keep getting a "error C2001: newline in constant" error....
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.