how to come out of file

K

kumar

i am bit confused about this code, why does the below while loop is
contineous
how can i come out of the file.
i am using linux machine


char ch;//char varaible
FILE *fp; // file pointer
fp=fopen("file","r");
while(fp!=EOF)// here i am just reading character by character and
printing to display
{
fscanf(fp,&ch);
printf("%c",ch);

}

problem is:: At end ,file pointer is pointing to last character and it
is holding that because of this
the while condition is true
so how can i come out of loop
 
B

Bartc

kumar said:
i am bit confused about this code, why does the below while loop is
contineous
how can i come out of the file.
i am using linux machine


char ch;//char varaible
FILE *fp; // file pointer
fp=fopen("file","r");
while(fp!=EOF)// here i am just reading character by character and
printing to display
{
fscanf(fp,&ch);
printf("%c",ch);

}

fp is unlikely to ever equal EOF; it's a file handle.

You might try:

while ( (ch=fgetc(fp))!=EOF)

and take out the fscanf line (which looks suspect anyway).

Also you should check that fp is valid after fopen(). And don't forget
fclose(fp) at the end.
 
J

Jens Thoms Toerring

kumar said:
i am bit confused about this code, why does the below while loop is
contineous
how can i come out of the file.
i am using linux machine
char ch;//char varaible

You better don't get in the habit of commenting the obvious.
FILE *fp; // file pointer
fp=fopen("file","r");
while(fp!=EOF)// here i am just reading character by character and

This completely off the mark and the compiler would tell so
you if you would raise its warning level to a suitable value.
You are comparing an int to a pointer and that makes no sense
at all. 'fp' will never change (at least as long as you don't
actively asign it a new value) and neither will EOF.

Instead you should check 'fp' for being NULL before using it
- it will be NULL in case opening the file failed.
printing to display
{
fscanf(fp,&ch);

Testing the return value of fscanf() will tell you when you
reached the end of the file. Since you read only a single
item you simply can test it for EOF (but also other problem
with reading the file will make it return EOF, if you want
to know the details use feof() or ferror() to distinguish
between those cases).

But using fscanf() is a bit like shooting sparrows with a canon,
have a look at (f)getc() for a probably much faster function for
reading single characters.
printf("%c",ch);

And here putchar() probably is more suitable.
problem is:: At end ,file pointer is pointing to last character and it

The file pointer isn't pointing to a char, maybe some pointer
in a struture 'fp' may be pointing to does (but that's imple-
mentation specific and you shouldn't care about that)
is holding that because of this
the while condition is true

The while condition is only true by accident. You're comparing
apples to bananas and since such a comparison isn't reasonable
you whatever the result is has no value for your program.
so how can i come out of loop

By testing the return values of functions that tell you some-
thing about the fact that the end of the file has been reached.
Typically these are the functions that do the reading and thus
notice that they couldn't read what you asked them to read.
Carefully read their descriptions and you will find it's rather
simple. Don't make unfounded assumptions like that the file
pointer would do anything.
Regards, Jens
 
J

Jens Thoms Toerring

fp is unlikely to ever equal EOF; it's a file handle.
You might try:
while ( (ch=fgetc(fp))!=EOF)

I might be useful to add that in this case 'ch' must be defined
as an int and not a char as the OP did, otherwise he might end
up with the same problem as before;-)
and take out the fscanf line (which looks suspect anyway).

In what respect? I guess it's not too clever to use fscanf()
for single character input, but "suspect" seems to be a bit
strong to me.
Regards, Jens
 
V

vippstar

i am bit confused about this code, why does the below while loop is
contineous
how can i come out of the file.
i am using linux machine

char ch;//char varaible
Don't comment the obvious, as suggested by others as well.
ch should be int. See question 12.1 of the C faq for an explanation.
FILE *fp; // file pointer
fp=fopen("file","r");
Test for the return value of fopen(). If fopen() fails, NULL is
returned.
while(fp!=EOF)// here i am just reading character by character and
Here you compare a pointer to an integer. What you actually want is:
while((ch = getc(fp)) != EOF)
printing to display
Also use /* */ comments when you post to usenet. Line wrapping breaks
code that uses // comments.
{
fscanf(fp,&ch);
fscanf's second argument is expected to be a null terminated array.
You are passing a pointer to a single char, also note the absence of
any variables to write to whatsoever.
 
J

Jens Thoms Toerring

In what respect? I guess it's not too clever to use fscanf()
for single character input, but "suspect" seems to be a bit
strong to me.

Sorry, I take that back! Of course, the way the OP used it is
isn't just suspect but plain wrong since the format string is
missing! I should have read it more carefully.

Regards, Jens
 
B

Bartc

Jens Thoms Toerring said:
I might be useful to add that in this case 'ch' must be defined
as an int and not a char as the OP did, otherwise he might end
up with the same problem as before;-)

Oops, yes. It looked just like some code of mine the other day, which come
to think of it was wrong too..
 
K

Keith Thompson

kumar said:
i am bit confused about this code, why does the below while loop is
contineous
how can i come out of the file.
i am using linux machine


char ch;//char varaible
FILE *fp; // file pointer
fp=fopen("file","r");
while(fp!=EOF)// here i am just reading character by character and
printing to display
{
fscanf(fp,&ch);
printf("%c",ch);

}

problem is:: At end ,file pointer is pointing to last character and it
is holding that because of this
the while condition is true
so how can i come out of loop

Your best bet is to read section 12 of the comp.lang.c FAQ,
<http://www.c-faq.com/>. throw away the code you've posted, and start
again.

It looks very similar to something that was posted here recently; I
assume that's where you got it. You should at least go back and look
at the responses to that article.

Here's what's wrong with the code you posted.

You posted a code fragment, not a complete program. There's no way
for us to tell whether the code you posted is even your real code.
Write a small, complete, self-contained program that illustrates your
problem, and post that *exactly* (use copy-and-paste or equivalent).

Avoid "//" comments in C code posted to Usenet. Line-wrapping can
turn them into syntax errors.

You don't check whether fopen() succeeded or not.

The comparison ``fp!=EOF'' is nonsensical. Your compiler should have
rejected it, or at least warned you about it. Did you get a warning
from your compiler? If so, *don't just ignore it*, and at least tell
us about it. If not, find out how to get your compiler to produce
more warnings.

Your fscanf call is nonsensical. The second parameter is (a pointer
to) a format string. Don't even think about using a function without
first reading the documentation.

Your code shows signs that you're guessing how things work. Don't do
that. If you need to know how a function works, consult your
textbook, or your online documentation, or even the standard.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top