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

Code comments solicited

This is intended to be a simple version of the Unix "head" command,
i.e. a utility that displays the first n lines of a file. Comments
welcomed...

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

using namespace std;

int main( int argc, char *argv[] )
{
if( argc < 2 || argc > 3 ) {
// endl preferable to "\n" here?
cerr << "Usage: " << argv[0] << " <file> [lines]" << endl;
return EXIT_FAILURE;
}
ifstream f( argv[1] );
unsigned int count;
if( argc == 3 ) {
// as suggested in responses to my question about atoi()
(stringstream(argv[2])) >> count; // no error checking! :)
}
else {
count=10;
}
if( !f ) {
// endl preferable to "\n" here?
cerr << "Could not open file: " << argv[1] << endl;
return EXIT_FAILURE;
}
string s;
while( !f.eof() && count-- ) {
getline( f, s );
// "\n" preferable?
cout << s << "\n";
}
f.close();
return EXIT_SUCCESS;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
13 1379
Christopher Benson-Manica wrote:
[snip] string s;
while( !f.eof() && count-- ) {
getline( f, s );


eof is not intended to be used the way you use it here.

The pattern to read a file in C++ is:

while ( read_as_long_as_there_is_no_error ) {
do somthing with the thing read
}

what was the error? if it was eof, then everything is fine.

-------------------------

In your case there is no need to check for eof after
the loop, because not having read to the end of loop
is fine and expected.

Thus:

while( count-- && getline( f, s ) )
cout << s << '\n';

//
// normally you would do
//
// if( !f.eof() )
// cout << "There was an error reading the file\n";
//
Also: you don't check if the conversion from argv[2] into an
int has worked. But I'm sure you already know this.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while( count-- && getline( f, s ) )
cout << s << '\n'; // if( !f.eof() )
// cout << "There was an error reading the file\n";
<denseness>Wait, explain again why this isn't needed...?</denseness>
Also: you don't check if the conversion from argv[2] into an
int has worked. But I'm sure you already know this.


Yes, I indicated this in a comment :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bu**********@chessie.cirr.com...
Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while( count-- && getline( f, s ) )
cout << s << '\n';

// if( !f.eof() )
// cout << "There was an error reading the file\n";


<denseness>Wait, explain again why this isn't needed...?</denseness>


Because the error bit in stream is set after reading the EOF.
Thus your program runs one extra iteration.

Best wishes,
Sharad
Jul 22 '05 #4
Christopher Benson-Manica wrote:

Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while( count-- && getline( f, s ) )
cout << s << '\n';

// if( !f.eof() )
// cout << "There was an error reading the file\n";


<denseness>Wait, explain again why this isn't needed...?</denseness>


OK.

First of all:
A stream enters eof state *after* it has tried (and failed) to read
past the end of file. Thus a loop like:

while( !f.eof() ) {
getline ...
do something with the read line
}

will not work. Only after getline has determined that eof was reached,
the next call to eof will return true. But then it's to late, you already
have tried to 'do something with the read line', although there wasn't
a line read.

So what you can do is:

while( !f.eof() ) {
getline ...
if( !f.eof() ) {
do something with the read line
}
}

That will work. But, it only accounts for eof errors (and is ugly). It does
not account for other errors during the read (eg. corrupt floppy, eg...)

But there is a workaround for this. All the read functions return a status
or something that can be used as a status, indicating: read was successfull
or read was not successfull. Thus you change the above to:

while( getline .... )
do something with the read line

That while loop will terminate when a read fails. Now there are many resons
why a read can fail, one of them is: because the read attempts to read
past eof. So normally you test *after* the loop, why the loop has terminated:

while( getline .... )
do something with the read line

if( ! f.eof() )
there was a reading error. The loop should only terminate
because of eof.

Of course this assumes that the whole file needs to be read. But in your
case this is not the case: you only want to read some n lines from the
file, thus you may or may not reach eof when doing so. Thus the test for
eof alone has no significance (But a combination of the streams state and
eof has!):

if( !f && !f.eof() ) { // if the stream has detected a problem AND
// this problem was not eof, then something
// else must have occoured
cout << "Reading error\n";
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bu**********@chessie.cirr.com...
Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while( count-- && getline( f, s ) )
cout << s << '\n';
// if( !f.eof() )
// cout << "There was an error reading the file\n";


<denseness>Wait, explain again why this isn't needed...?</denseness>


Your original code looked like this:

while( !f.eof() && count-- ) {
getline( f, s );
}

This actually introduces a kind of unrequired redundancy because you imply
that the contents of the loop is to be executed if count has not yet reached
the lower limit OR the end of file has not yet been reached. If both are
true you attempt to read from the file. Getline actually returns a reference
to the input stream which has an overloaded conversion for void* and bool.
These conversion functions test for the failbit which has been automatically
set if EOF has occured. Thus you can implicitely use the getline statement
to check for EOF and read from the file if EOF has not occured.

Hence,

while( getline(f, s) && count-- ) {
.....
}

do the trick.
Also: you don't check if the conversion from argv[2] into an
int has worked. But I'm sure you already know this.


Yes, I indicated this in a comment :)


Instead of just commenting on this issue I'd recommend to solve it, for if
there is one thing a developer can expect then it is the unexpected. A
possible way to test for the result of a string conversion is to use the
following function:

////////////////////////////////////////////////////////////////////////////
//
template<typename T>
T FromStringEx( const std::string& s, bool& bSuccess)
// string conversion from string to typename T with a flag
// the represents success or failure.
//
// e.g: int d = FromString<int>( s );
// e.g: string s = ToString( d );
//
// if you want to check whether the conversion was successful then you can
// use the bSuccess flag!
////////////////////////////////////////////////////////////////////////////
//
{
bSuccess = true;
std::istringstream is(s);
T t;
is >> t;
if( !is )
bSuccess = false;
return t;
}

Regards
Chris
Jul 22 '05 #6
On Mon, 19 Jan 2004 13:10:06 +0000 (UTC) in comp.lang.c++, Christopher
Benson-Manica <at***@nospam.cyberspace.org> was alleged to have written:
if( !f ) {
// endl preferable to "\n" here?
cerr << "Could not open file: " << argv[1] << endl;


if (!f)
perror(argv[1]);

Jul 22 '05 #7
In article <bu**********@chessie.cirr.com>, at***@nospam.cyberspace.org
says...

[ ... ]
while( !f.eof() && count-- ) {
getline( f, s );
// "\n" preferable?
cout << s << "\n";
}


I think a for loop is really suitable here:

for (int i=0; i<count && getline(f,s); i++)
std::cout << s << "\n";

IMO, this indicates the real intent a bit better: that you normally
intend to execute a specific number of iterations. You might exit the
loop early, but you really don't expect that as a rule. To make that
even more clear, you _might_ consider something like this:

for (int i=0; i<count; i++) {
if (!getline(f,s))
break;
std::cout << s << "\n";
}

A purist on structure would point out that this breaks the single-entry,
single-exit structure, but IMO, that's more or less a red herring.
Structure is useful to the extent that it improves readability, and if
breaking pure structure doesn't hurt readability (which I would say is
true here) then I wouldn't worry much about it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #8
On Mon, 19 Jan 2004 21:13:37 GMT in comp.lang.c++, Jerry Coffin
<jc*****@taeus.com> was alleged to have written:
while( !f.eof() && count-- ) {
getline( f, s );
// "\n" preferable?
cout << s << "\n";
}


I think a for loop is really suitable here:

for (int i=0; i<count && getline(f,s); i++)
std::cout << s << "\n";

IMO, this indicates the real intent a bit better: that you normally
intend to execute a specific number of iterations. You might exit the
loop early, but you really don't expect that as a rule.


Well, I have to disagree on the style issue. Leaving the loop due to
eof and leaving due to the count are exactly equally normal, expected,
and valid. The decrementing count is well-understood, and adding the
variable i adds unnecessary entities. So, I prefer the while().

Of course as noted by various posters, f.eof() in the condition is bad.
This issue is covered in topic "[15.4] Why does my input seem to process
past the end of file?" of Marshall Cline's C++ FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #9
David Harmon wrote:
On Mon, 19 Jan 2004 21:13:37 GMT in comp.lang.c++, Jerry Coffin
<jc*****@taeus.com> was alleged to have written:
while( !f.eof() && count-- ) {
getline( f, s );
// "\n" preferable?
cout << s << "\n";
}
I think a for loop is really suitable here:

for (int i=0; i<count && getline(f,s); i++)
std::cout << s << "\n";

IMO, this indicates the real intent a bit better: that you normally
intend to execute a specific number of iterations. You might exit the
loop early, but you really don't expect that as a rule.


Well, I have to disagree on the style issue. Leaving the loop due to
eof and leaving due to the count are exactly equally normal, expected,
and valid.


The program is intended to read the first 'count' lines, and so the
"normal" thing is to read 'count' lines (a for loop sounds good for
this). However, the loop might stop (i.e. being broken out) earlier if
some error condition (like eof) is met.
The decrementing count is well-understood, and adding the
variable i adds unnecessary entities. So, I prefer the while().


I don't see the connection between those two.

Jul 22 '05 #10
On Tue, 20 Jan 2004 01:34:51 +0100 in comp.lang.c++, Rolf Magnus
<ra******@t-online.de> was alleged to have written:
The program is intended to read the first 'count' lines, and so the
"normal" thing is to read 'count' lines (a for loop sounds good for
this). However, the loop might stop (i.e. being broken out) earlier if
some error condition (like eof) is met.


Or in other words, you agree with Jerry.
The decrementing count is well-understood, and adding the
variable i adds unnecessary entities. So, I prefer the while().


I don't see the connection between those two.


I assume the variable i was suggested by the usual for-loop idiom.
Without it, the for loop simplifies down to
for ( ; getline(f,s) && count-- ; )

To me that is simply an uglier way of writing while().

Jul 22 '05 #11
David Harmon wrote:
On Tue, 20 Jan 2004 01:34:51 +0100 in comp.lang.c++, Rolf Magnus
<ra******@t-online.de> was alleged to have written:
The program is intended to read the first 'count' lines, and so the
"normal" thing is to read 'count' lines (a for loop sounds good for
this). However, the loop might stop (i.e. being broken out) earlier if
some error condition (like eof) is met.


Or in other words, you agree with Jerry.


Yes. I just wanted to explain the reason for that.
The decrementing count is well-understood, and adding the
variable i adds unnecessary entities. So, I prefer the while().


I don't see the connection between those two.


I assume the variable i was suggested by the usual for-loop idiom.
Without it, the for loop simplifies down to
for ( ; getline(f,s) && count-- ; )

To me that is simply an uglier way of writing while().


I was rather thinking of:

for ( ; count; count--)
if (!getline(f, s)) break;

But you're right, it looks a bit odd.

Jul 22 '05 #12
In article <40****************@news.west.earthlink.net>,
so****@netcom.com says...

[ ... ]
Well, I have to disagree on the style issue. Leaving the loop due to
eof and leaving due to the count are exactly equally normal, expected,
and valid.
Here's where we have a fundamental disagreement. I'd guess that well
over 99% of the time people use head, they expect to see exactly the
number of lines specified, NOT fewer. As such, I'd say the early exit
is an unusual enough condition that giving it separate and special
processing is perfectly reasonable.
The decrementing count is well-understood, and adding the
variable i adds unnecessary entities. So, I prefer the while().


A decrementing loop is reasonably well understood, but certainly not
nearly AS well understood or widely used. Adding an "unnecessary
entity" is usually done to make code simpler and easier to understand,
and IMO that's what happens here.

Consider: the primary (ultimately, ONLY) use of typedef is to add
"entities" that are, strictly speaking, unnecessary. Nonetheless, if
you're stuck with declaring a pointer to a function that returns a
pointer to an array of pointers to functions, each of which returns a
pointer to a function that returns an array of int's, you've got two
choices: introduce (at least) one "unnecessary entity", or write lousy
code.

This case isn't nearly that extreme, but in the end you're left with one
thing: if you expect a loop to execute a specific number of times (which
is exactly the case here, regardless of your claim to the contrary) then
you should normally use a for loop. You should use an incrementing
counter unless you have a specific reason to do otherwise, typically
involving using that variable to index into an array, vector, etc.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #13
On Wed, 21 Jan 2004 04:06:18 GMT in comp.lang.c++, Jerry Coffin
<jc*****@taeus.com> was alleged to have written:
Adding an "unnecessary entity" is usually done to make code simpler
and easier to understand, and IMO that's what happens here.
I would certainly agree with the rest if I thought that was the case
here. Few things are worth more in programming than clarity.
Consider: the primary (ultimately, ONLY) use of typedef is to add
"entities" that are, strictly speaking, unnecessary.


This example lifted from a thread in comp.lang.c++.moderated

typedef const char (&r2a)[10];

struct foo {
operator r2a() const; //cannot be declared without the typedef
};

Which says something about C++ syntax.

Jul 22 '05 #14

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

Similar topics

2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
22
by: Illya Havsiyevych | last post by:
Hello How easily parse VB/VBA code by VB/VBA code ? Is any ready solutions ? Thank's, illya
28
by: Steve Jorgensen | last post by:
I often come up with logic like this somewhere in a function: .... If Not IsNull(<some expression>) Then <default action> Else <alternative action> End If ....
46
by: Profetas | last post by:
Hi, I know that this is off topic. but I didn't know where to post. Do you comment your source code while coding or after coding. for example: you write a procedure and after it is...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
4
by: Vinayak | last post by:
Dear Members I'm new here. Please permit me to ask some newbie question I'm from a non-profit organization, working for gender equality. We wish to get a small message across to sister blogs...
0
by: vighnesh | last post by:
Hi All Can Anybody Please guide me in clearing the Internet Explorer History ( Browser History ) through VB.NET code. Thanks in advance Regards Vighneswar NGV33010
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.