By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

File Viewer that stops at the 24th line

P: n/a
Hello Everyone,

I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?

Here is the code....

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
FILE *fp;
char buf[24];

if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");
system("PAUSE");
}

else
{
fp = fopen(argv[1],"r");
while (!feof(fp))
{
puts(buf);
fgets(buf,sizeof(buf)-1,fp);

}

}

return 0;

}
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
sp***@rloteck.com (Rafael) writes:
I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?


Increment a counter for each line you output. When the counter
reaches 24, stop.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Nov 13 '05 #2

P: n/a
sp***@rloteck.com (Rafael) wrote:
Hello Everyone,

I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?

Here is the code....

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for strchr(), see below */
#define NUM_LINES 24 /* see below */

int main(int argc, char *argv[])
{
int i;
FILE *fp;
char buf[24];

if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");
system("PAUSE");
}

else
{
fp = fopen(argv[1],"r");
while (!feof(fp))
That's not the recommended way to iterate through a file.
{
puts(buf); You are printing the contents of buf before having filled it with
sensible data. IOW: you print garbage the first time your loop runs.
fgets(buf,sizeof(buf)-1,fp); You don't need to subtract one from the buffer size.

}
Since you have already declared a variable 'i', you could use it
to count the lines and change the loop to:

i = 0;
while ( i < NUM_LINES && fgets( buf, sizeof buf, fp ) )
{
fputs( buf, stdout );
if ( strchr( buf, '\n' ) )
++i;
}

Note the use of fputs, otherwise two newline characters would be
written after each line: the one read from file by fgets and the
one automatically appended by puts. Also note the use of the
#define'd constant NUM_LINES instead of the 'magic number' 24.

The "if (strchr( ...))" construct ensures that the counter is
incremented only after a whole line has been read. BTW, you may
consider to use a bigger buffer.

}

return 0;

}


Another solution: forget about the buffer and read/write
character-wise, incrementing the line counter every time you hit
'\n'.

HTH. Now, what do I get for doing your homework? ;-)

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3

P: n/a
On 30 Nov 2003 13:55:55 -0800, sp***@rloteck.com (Rafael) wrote:
Hello Everyone,

I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?

Here is the code....

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
FILE *fp;
char buf[24];
buf is uninitialized. Its value is indeterminant.

if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");
system("PAUSE");
}

else
{
fp = fopen(argv[1],"r");
Please learn to indent consistently. It will make you life and ours
so much easier. The else is at the same level as the if and correctly
indented the same. The braces and statements that form the else block
should be indented the same as those of the if.
while (!feof(fp))
{
puts(buf);
On the first pass through the loop, buf is still uninitialized. Its
use in this context invokes undefined behavior.

If you initialized buf before the loop, this still has an undesirable
aspect. When the input to fgets contains a '\n' (such as at the end
of each line of the file), fgets includes that character in the
buffer. If the buffer is full before the '\n' is encountered, fgets
does not add one (it cannot; there is only room left for the
terminating '\0'). puts always adds a '\n' to the output.
Consequently, some (most?) of your output will be double spaced while
the rest will be single spaced.

The usual fix is to search the input for a '\n' (e.g., strchr) and, if
found, replace it with a '\0' since it is guaranteed to be the last
non-terminal character in the string.
fgets(buf,sizeof(buf)-1,fp);
fgets stops reading after it reads the length-1 characters so you
should use sizeof buf (the parentheses are unnecessary).
}

}

return 0;

}


<<Remove the del for email>>
Nov 13 '05 #4

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> wrote in message news:<sg********************************@4ax.com>. ..
sp***@rloteck.com (Rafael) wrote:
Hello Everyone,

I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?

Here is the code....

#include <stdio.h>
#include <stdlib.h>


#include <string.h> /* for strchr(), see below */
#define NUM_LINES 24 /* see below */

int main(int argc, char *argv[])
{
int i;
FILE *fp;
char buf[24];

if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");
system("PAUSE");
}

else
{
fp = fopen(argv[1],"r");
while (!feof(fp))


That's not the recommended way to iterate through a file.
{
puts(buf);

You are printing the contents of buf before having filled it with
sensible data. IOW: you print garbage the first time your loop runs.
fgets(buf,sizeof(buf)-1,fp);

You don't need to subtract one from the buffer size.

}


Since you have already declared a variable 'i', you could use it
to count the lines and change the loop to:

i = 0;
while ( i < NUM_LINES && fgets( buf, sizeof buf, fp ) )
{
fputs( buf, stdout );
if ( strchr( buf, '\n' ) )
++i;
}

Note the use of fputs, otherwise two newline characters would be
written after each line: the one read from file by fgets and the
one automatically appended by puts. Also note the use of the
#define'd constant NUM_LINES instead of the 'magic number' 24.

The "if (strchr( ...))" construct ensures that the counter is
incremented only after a whole line has been read. BTW, you may
consider to use a bigger buffer.

}

return 0;

}


Another solution: forget about the buffer and read/write
character-wise, incrementing the line counter every time you hit
'\n'.

HTH. Now, what do I get for doing your homework? ;-)

Regards


Thanks dude, you are the boom......: )

Rafael
Nov 13 '05 #5

P: n/a
"Rafael" <sp***@rloteck.com> wrote in message
news:a5**************************@posting.google.c om...
Hello Everyone,

I need some major help with this code I have for a file viewer. I
need it to stop at the 24th line. What Syntax do I need for this to
happen?

Here is the code....

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
FILE *fp;
char buf[24];

if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");

Not to the point, but maybe practical:
printf("ERROR: Please enter a file name afer %s.\n", argv[0]);
Nov 13 '05 #6

P: n/a
"nobody" <no****@nowhere.non> wrote:
"Rafael" <sp***@rloteck.com> wrote:

<snip>
if(argv[1]==NULL)
{
printf("ERROR: Please enter a file name afer char.exe or
char.\n");

Not to the point, but maybe practical:
printf("ERROR: Please enter a file name afer %s.\n", argv[0]);


Which will print

ERROR: Please enter a file name afer .

if the host environment doesn't provide the program name in
argv[0] (C99 5.1.2.2.1#2 :-).

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7

P: n/a
On Tue, 02 Dec 2003 16:19:54 +0100, Irrwahn Grausewitz
<ir*******@freenet.de> wrote:
"nobody" <no****@nowhere.non> wrote:

[error message guessing at program name]
Not to the point, but maybe practical:
printf("ERROR: Please enter a file name afer %s.\n", argv[0]);


Which will print

ERROR: Please enter a file name afer .

if the host environment doesn't provide the program name in
argv[0] (C99 5.1.2.2.1#2 :-).

or commit Undefined Behavior if the implementation chooses to make
argc == 0 and hence argv[0] == NULL, as permitted in that same section
if there are no (usually command-line) parameters (made) available.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.