program for reading a file

B

broli

I cannot understand why the program goes into infinite loop after the
file is printed on the screen.

#include<stdio.h>

int main(void)
{

FILE *fp;
char c;

fp = fopen("sample.dat", "r");
if(fp == NULL)
{

printf("Cannot open the file\n");
return 0;

}
while(1)
{
c = fgetc(fp);

if(c == EOF)
break;
else
printf("%c",c);

}
fclose(fp);

return 0;

}

sample.dat

as;djasjd;asjd
asdsaddskaslkldsklasdklsdklaklasdklaskldaskl
;lkasdasdkksdlaklasdklsad;klasd;klaskdl;
 
W

Willem

broli wrote:
) I cannot understand why the program goes into infinite loop after the
) file is printed on the screen.

<snip>

) FILE *fp;
) char c;

Here's the error.

<snip>

) c = fgetc(fp);
) if(c == EOF)

EOF is not a value that fits in a char.
c should be an int, not a char.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
S

santosh

broli said:
I cannot understand why the program goes into infinite loop after the
file is printed on the screen.

#include<stdio.h>

int main(void)
{

FILE *fp;
char c;

fp = fopen("sample.dat", "r");
if(fp == NULL)
{

printf("Cannot open the file\n");
return 0;

}
while(1)
{
c = fgetc(fp);

Here is your first mistake. Fgetc returns an int value, not a char. This
is because the out-of-band value EOF is an int. You need to store
fgetc's return value in an int, test it if it is EOF and assign to a
char only if it is an ordianary character.
if(c == EOF)

This check will fail because of what I said above.
 
B

broli

EOF is not a value that fits in a char.
c should be an int, not a char.

Yes now I recall reading this in K & R 2. Strangely I found this wrong
approach( char c = EOF) being used in many online tutorials.
 
B

broli

After changing to int, Im still getting an infinite loop -

#include<stdio.h>

int main(void)
{

FILE *fp;
int c;

fp = fopen("sample.dat", "r");
if(fp == NULL)
{

printf("Cannot open the file\n");
return 0;

}

while(1)
{

c = fgetc(fp);
if(c == EOF)
{
break;
}
else
printf("%c",c);

}

return 0;

}
 
M

Morris Dovey

broli said:
I use TC 2.01 on win xp and to be honest it has given me many
problems. I want to use a different and easier complier, I have looked
around but cannot find one.

I don't have XP, but use TC3 under 95, 98, and ME without problem
- but much prefer to boot up Linux and use gcc.

Actually, I don't think you'll find anything very much easier to
use than your Turbo C package. I think version 3 is available for
free download if you think it might solve any of your problems.

My favorite trick is making changes in an edit window, then
compiling, linking, and running the program without having saved
my changed source file. :p
 
F

Flash Gordon

broli wrote, On 15/03/08 09:51:
Yes now I recall reading this in K & R 2. Strangely I found this wrong
approach( char c = EOF) being used in many online tutorials.

That just shows you why you should not rely on random online tutorials.
Within the past week or so there was a question about tutorials and
links to the few known good ones were posted.
 
R

Richard Heathfield

broli said:
After changing to int, Im still getting an infinite loop -

If you don't *want* an infinite loop, why use while(1)?

while((c = getc(fp)) != EOF)
{
putchar(c);
}

Having said that, the code you posted looks like it should work just fine.
I suspect that it is somewhat different from the code you executed.
 
B

broli

I use TC 2.01 on win xp and to be honest it has given me many
problems. I want to use a different and easier complier, I have looked
around but cannot find one.
 
S

santosh

broli said:
I use TC 2.01 on win xp and to be honest it has given me many
problems. I want to use a different and easier complier, I have looked
around but cannot find one.

Try MinGW, Cygwin, Borland's free command line compiler or Microsoft's
Express 2008, or lcc-win32 or PellesC etc.

You are spoiled for choice!
 
B

broli

Try MinGW, Cygwin, Borland's free command line compiler or Microsoft's
Express 2008, or lcc-win32 or PellesC etc.

You are spoiled for choice!

lcc-win32.. is it freely available on jacob navias website ?
 
R

Richard Heathfield

broli said:
I use TC 2.01 on win xp and to be honest it has given me many
problems.

I think it's probably the other way around. :) I've just tested the
program you posted, the one with:

int c;

and

while(1)
{
c = fgetc(fp);

etc., using Turbo C 2.01 under Windows XP, and it works just fine.
 
R

Richard Heathfield

Morris Dovey said:
I don't have XP, but use TC3 under 95, 98, and ME without problem
- but much prefer to boot up Linux and use gcc.

Actually, I don't think you'll find anything very much easier to
use than your Turbo C package. I think version 3 is available for
free download if you think it might solve any of your problems.

The program he posted works just fine using Turbo C 2.01 under Windows XP.
I'm not sure what his problem is, but the problem isn't with the program,
the implementation, or the operating system.
My favorite trick is making changes in an edit window, then
compiling, linking, and running the program without having saved
my changed source file. :p

Ah, but that's what make is for. When make says "'target' is up to date",
you know you've forgotten something!
 
W

WANG Cong

On Sat, 15 Mar 2008 03:02:08 -0700,broli wrote:
After changing to int, Im still getting an infinite loop -

This program works perfectly fine here. I am using gcc on an
i386 Linux.

What's your problem there and what's your environment?
 
B

Bartc

santosh said:
Try MinGW, Cygwin, Borland's free command line compiler or Microsoft's
Express 2008, or lcc-win32 or PellesC etc.

You are spoiled for choice!

gcc -Wall (mingw version) doesn't give a warning about char c=fgetc().

lcc-win32 -A does give a warning.

Pelles C (or any other running under IDE) I wouldn't have a clue how to set
warning level.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top