472,353 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Faulty while loop

Why does my while loop print the last line of the file twice?
Thanks.

void ReadFile(FILE *fp)
{
char buff[MAX_LEN];

while (!feof(fp)) {
fgets(buff, MAX_LEN, fp);
printf("%s", buff);
}
}
Nov 15 '05 #1
2 1363
On Sat, 29 Oct 2005 02:36:40 GMT, bildad wrote:
Why does my while loop print the last line of the file twice?
Thanks.

void ReadFile(FILE *fp)
{
char buff[MAX_LEN];

while (!feof(fp)) {
fgets(buff, MAX_LEN, fp);
printf("%s", buff);
}
}


Never mind. I should have read posts before sending. Sorry.
Nov 15 '05 #2
On 2005-10-29, bildad <bi****@wi.rr.com> wrote:
Why does my while loop print the last line of the file twice?
Thanks.

void ReadFile(FILE *fp)
{
char buff[MAX_LEN];

while (!feof(fp)) {
fgets(buff, MAX_LEN, fp);
printf("%s", buff);
}
}


Because you're using feof() to check for an end-of-file condition at
the top of the loop. Using the return value of fgets() instead, or
(i believe) checking at the end of the loop, would both solve your
problem.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple...
7
by: Anjali M | last post by:
#include<stdio.h> int main() { int a = 10; switch(a) { case '1': printf("ONE\n"); break; case '2':
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin...
3
by: Neil Cerutti | last post by:
How do I cope with faulty encoding settings? I'm writing an application that needs all internal character data to be stored in iso-8859-1. It...
16
by: shana07 | last post by:
What's that mean by faulty program in java? I am told that it is any wrong in calculation for numerical program. Really appreciate if someone could...
166
by: Nimmi Srivastav | last post by:
Apologies if my cross posting has offended anyone.... For a pure hobbyist C/C++ programmer, who wants to develop applications to run on Windows,...
5
ashitpro
by: ashitpro | last post by:
There are 7 machines, each of them produces the coin of weight 2 grams. Someday somehow one machine gets faulty and started producing coin of weight...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.