File Viewer that stops at the 24th line

R

Rafael

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;

}
 
B

Ben Pfaff

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.
 
I

Irrwahn Grausewitz

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))

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
 
B

Barry Schwarz

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>>
 
R

Rafael

Irrwahn Grausewitz said:
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))

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
 
N

nobody

Rafael said:
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]);
 
I

Irrwahn Grausewitz

nobody said:
"Rafael" <[email protected]> wrote:
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
 
D

Dave Thompson

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top