473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(a rgv[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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
13 1413
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_er ror ) {
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******@gasca d.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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:bu******** **@chessie.cirr .com...
Karl Heinz Buchegger <kb******@gasca d.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******@gasca d.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

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:bu******** **@chessie.cirr .com...
Karl Heinz Buchegger <kb******@gasca d.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<typena me 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::istringstr eam 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.c yberspace.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**********@c hessie.cirr.com >, at***@nospam.cy berspace.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

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

Similar topics

2
2363
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 http-equiv="Content-Type" content="text/html;
22
7104
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
2524
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
2467
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 working, you'll comment it.
192
9448
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
1927
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 (blogs with a common thread, purpose) *I am SERIOUSLY AGAINST SPAM of any kind*
0
2749
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
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.