473,471 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File Viewer that stops at the 24th line

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
7 1348
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
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
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
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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
6
by: Codemonkey | last post by:
Hi, I have a few questions about best practices when it comes to the management of temporary files. Any thoughts anyone can give would be much appreciated. Basically, I'm writing a document...
9
by: Mike | last post by:
How do I prevent SQL Server 2000 from posting successful backup completion messages to the Windows 2000 Application Event Log? I have scheduled jobs which backup my transaction logs on 50+...
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how...
7
by: Michele | last post by:
Hi, i need an ascii file viewer. How can i find the source or documentation for it? Naturally in c# Thanks and sorry for my bad english
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
0
by: chrisexv6 | last post by:
Running a webservice under IIS 5.0, we've recently started seeing COM+ errors in Event Viewer. The details are: Event Type: Error Event Source: COM+ Event Category: SVC Event ID: 4194...
2
by: Gerry | last post by:
Python 2.5, Windows XP. I have a 48-line text file written by a Windows python script, I try to read it as follows: f = open ("depstats.txt", "r", 0) for index, line in...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.