Since you are the end of the file, you need to seek to the beginning of the file again.
(Just as you finished reading a book, you need to travel to the beginning of the book again if you want to continue reading.)
Printable View
Since you are the end of the file, you need to seek to the beginning of the file again.
(Just as you finished reading a book, you need to travel to the beginning of the book again if you want to continue reading.)
So, yeah, try fseek. Or just fclose() and fopen() the file again (fseek is more correct).
Also fix your codeing style. It should be like this:
You MUST have correct the spaces and the {,} or the code is difficult to read. The above code is simple, but why not make it more readable?Code:else
{
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. */
{
for (ch = getc(file); ch != EOF; ch = getc(file))
putc(ch, newfile);
}
char line[LINE_LEN], linein[NAME_LEN],lineout[NAME_LEN];
for (status = fgets(line, LINE_LEN, file);
status != 0;
status = fgets(line, LINE_LEN, file))
{
if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
fprintf(newfile, "%3>> %s\n", ++i, line);
}
}
If it is a copy/paste problem, my apologies :)
Thanks for the help and tips. So when using fseek() do I need to tell the program anything additional, or will this work (and should I look for the beginning of the original file or 'newfile'?
Code:fseek (file, 0L, SEEK_SET);
char line[LINE_LEN], linein[NAME_LEN],lineout[NAME_LEN];
for (status = fgets(line, LINE_LEN, file);
status != 0;
status = fgets(line, LINE_LEN, file))
{
if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
fprintf(newfile, "%3>> %s\n", ++i, line);
}
}
No, that simply fseek is correct. But consider replacing that for with while:
Code:while ( (status = fgets(line, LINE_LEN, file)) != NULL )
Thanks Elysia...and for the record I did change that 0 to NULL when you suggested it earlier...The code compiles fine but when I execute I get a windows error? Any ideas? I'm going to try on another pc, just curious...
What error and where?
The popup "windows encountered an error and must close the program..." It happens after I compile and run the code...
Happens on the laptop too!!! Hmmm......
fprintf(newfile, "%3>> %s\n", ++i, line);
The format is incorrect. What are you trying to write?
%3 is not a format type.
I'm trying to insert line numbers for ever line copied from the original document into every line in the new document, with the exception of the heading...
Okay- so the error went away and I get the line numbers to print. Must have been bad code in
there somewhere, but don't ask me where. Unfortunately the output is duplicated instead of
replaced...Hmmm...
The output looks like this:Code:{
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. */
{
for (ch = getc(file);ch != EOF; ch = getc(file))
putc(ch, newfile);
}
fseek (file, 0L, SEEK_SET); //return to the beginning of the file
char line[LINE_LEN], namein[NAME_LEN],nameout[NAME_LEN];
while ( (status = fgets(line, LINE_LEN, file)) != NULL )
{
if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
fprintf(newfile, "%d\t%s\n", ++i, line);
}
}
***end of first pass through, I think this is the putc()?***
Then it moves on to the second pass through... fseek()?Code:If oil prices stay high as expected, the revenues would create a substantial surplus for Iraq at a
time when Americans are facing an economic slump, aggravated by painfully high gas prices.
Click to Print Find this article at:
Copyright ? 2008 Time Inc. All rights reserved.
Code:1 Military Dealing With High Gas Prices
2 By AP/ANNE FLAHERTY
3
4 (WASHINGTON) — Consumers at the gas pump aren't the only ones suffering sticker
5 shock. Military units...