Connecting Tech Pros Worldwide Forums | Help | Site Map

File Viewer that stops at the 24th line

Rafael
Guest
 
Posts: n/a
#1: Nov 13 '05
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;

}

Ben Pfaff
Guest
 
Posts: n/a
#2: Nov 13 '05

re: File Viewer that stops at the 24th line


spawn@rloteck.com (Rafael) writes:
[color=blue]
> 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?[/color]

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
Irrwahn Grausewitz
Guest
 
Posts: n/a
#3: Nov 13 '05

re: File Viewer that stops at the 24th line


spawn@rloteck.com (Rafael) wrote:[color=blue]
> 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>[/color]

#include <string.h> /* for strchr(), see below */
#define NUM_LINES 24 /* see below */
[color=blue]
>
> 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))[/color]

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

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.
[color=blue]
>
> }
>
> return 0;
>
> }[/color]

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
(irrwahn33@freenet.de)
Barry Schwarz
Guest
 
Posts: n/a
#4: Nov 13 '05

re: File Viewer that stops at the 24th line


On 30 Nov 2003 13:55:55 -0800, spawn@rloteck.com (Rafael) wrote:
[color=blue]
>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];[/color]

buf is uninitialized. Its value is indeterminant.
[color=blue]
>
> 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");[/color]

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.
[color=blue]
> while (!feof(fp))
> {
> puts(buf);[/color]

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.
[color=blue]
> fgets(buf,sizeof(buf)-1,fp);[/color]

fgets stops reading after it reads the length-1 characters so you
should use sizeof buf (the parentheses are unnecessary).[color=blue]
>
> }
>
>}
>
> return 0;
>
>}[/color]



<<Remove the del for email>>
Rafael
Guest
 
Posts: n/a
#5: Nov 13 '05

re: File Viewer that stops at the 24th line


Irrwahn Grausewitz <irrwahn33@freenet.de> wrote in message news:<sg0lsvc7a0p0lmafc2eujt9o6j052aghn6@4ax.com>. ..[color=blue]
> spawn@rloteck.com (Rafael) wrote:[color=green]
> > 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>[/color]
>
> #include <string.h> /* for strchr(), see below */
> #define NUM_LINES 24 /* see below */
>[color=green]
> >
> > 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))[/color]
>
> That's not the recommended way to iterate through a file.
>[color=green]
> > {
> > puts(buf);[/color]
> You are printing the contents of buf before having filled it with
> sensible data. IOW: you print garbage the first time your loop runs.
>[color=green]
> > fgets(buf,sizeof(buf)-1,fp);[/color]
> You don't need to subtract one from the buffer size.
>[color=green]
> >
> > }[/color]
>
> 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.
>[color=green]
> >
> > }
> >
> > return 0;
> >
> > }[/color]
>
> 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[/color]

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

Rafael
nobody
Guest
 
Posts: n/a
#6: Nov 13 '05

re: File Viewer that stops at the 24th line


"Rafael" <spawn@rloteck.com> wrote in message
news:a55917dc.0311301355.4361c058@posting.google.c om...[color=blue]
> 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");
>[/color]
Not to the point, but maybe practical:
printf("ERROR: Please enter a file name afer %s.\n", argv[0]);


Irrwahn Grausewitz
Guest
 
Posts: n/a
#7: Nov 13 '05

re: File Viewer that stops at the 24th line


"nobody" <nobody@nowhere.non> wrote:[color=blue]
> "Rafael" <spawn@rloteck.com> wrote:[/color]
<snip>[color=blue][color=green]
> > if(argv[1]==NULL)
> > {
> > printf("ERROR: Please enter a file name afer char.exe or
> > char.\n");
> >[/color]
> Not to the point, but maybe practical:
> printf("ERROR: Please enter a file name afer %s.\n", argv[0]);[/color]

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
(irrwahn33@freenet.de)
Dave Thompson
Guest
 
Posts: n/a
#8: Nov 13 '05

re: File Viewer that stops at the 24th line


On Tue, 02 Dec 2003 16:19:54 +0100, Irrwahn Grausewitz
<irrwahn33@freenet.de> wrote:
[color=blue]
> "nobody" <nobody@nowhere.non> wrote:[/color]
[error message guessing at program name][color=blue][color=green]
> > Not to the point, but maybe practical:
> > printf("ERROR: Please enter a file name afer %s.\n", argv[0]);[/color]
>
> 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 :-).
>[/color]
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
Closed Thread