Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with Data Files

Reply
Thread Tools

problem with Data Files

 
 
alice
Guest
Posts: n/a
 
      01-18-2005
hi all,
What will happen when I tries to input a character from a file
which is finished, in other words what will 'c' contains after the last
statement in the following code is executed :

FILE * file =fopen("FILE1.c","r");
char c;
while((c=fgetc(file)) != EOF);
c=fgetc(file);
// what is value of c now???????????????????
Thanks,
Alice

 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      01-18-2005
On Tue, 18 Jan 2005 09:38:54 -0800, alice wrote:

> hi all,
> What will happen when I tries to input a character from a file
> which is finished, in other words what will 'c' contains after the last
> statement in the following code is executed :
>
> FILE * file =fopen("FILE1.c","r");
> char c;
> while((c=fgetc(file)) != EOF);


This is incorrect, c needs to be defined as int. fgetc() returns int and
EOF is not guaranteed to be representable as a char. If it is it won't be
distinct from all possible char values.

> c=fgetc(file);
> // what is value of c now??????????????????? Thanks,


Probably EOF again, maybe something else if the end-of-file condition
cleared in the meantime. End-of-file condition can be sticky but it
doesn't have to be. If you want to attempt further file I/O after
end-of-file is indicated you should perform a file seek operation if that
is appropriate or else call clearerr(). You should not simply attempt
another read operation.

Lawrence
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-18-2005
"bhanu" <> writes:

> EOF (-1)


EOF is often -1 but that value is not guaranteed. EOF may be any
negative `int' value.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-18-2005
alice wrote:
>
> hi all,
> What will happen when I tries to input a character from a file
> which is finished,
> in other words what will 'c' contains after the last
> statement in the following code is executed :
>
> FILE * file =fopen("FILE1.c","r");
> char c;
> while((c=fgetc(file)) != EOF);
> c=fgetc(file);
> // what is value of c now???????????????????


(char)EOF

--
pete
 
Reply With Quote
 
bhanu
Guest
Posts: n/a
 
      01-18-2005
Try this program: EOF is always -1, not any negative number.
#include <stdio.h>

int main()
{

FILE * file;
char c;

if((file=fopen("Test.txt","wb")) == NULL)
{
printf("error in opening the file");
return 0;
}

for(c = -10; c < 10; c++)
fputc(c, file);

fclose(file);

if((file=fopen("Test.txt","rb")) == NULL)
{
printf("error in opening the file");
return 0;
}

while((c=fgetc(file)) != EOF)
{
printf("\nc = %d", c);
}

c=fgetc(file);
printf("\n'c' After EOF = %d", c);

printf("\n");

fclose(file);
return 0;
}

Output:


c = -10
c = -9
c = -8
c = -7
c = -6
c = -5
c = -4
c = -3
c = -2
'c' After EOF = 0

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-18-2005
"bhanu" <> wrote in message
news: ups.com...
> Try this program: EOF is always -1,


It might be for a given compiler, it easily might not
be for others. The language imposes no requirement
that it be -1, only that it is an integer less than zero,
i.e. between INT_MIN and -1.

[snip code, which does not prove that EOF is always -1]

-Mike


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-18-2005
alice wrote:
>
> What will happen when I tries to input a character from a file
> which is finished, in other words what will 'c' contains after
> the last statement in the following code is executed :
>
> FILE * file =fopen("FILE1.c","r");
> char c;
> while((c=fgetc(file)) != EOF);
> c=fgetc(file);
> // what is value of c now???????????????????


First of all c must be of type int, not char. EOF is outside the
range of chars. With that change, your final c will _probably_ be
EOF, since most disk files have what is called sticky eof. If the
input file happens to be a terminal, it may not have sticky EOF and
anything can happen.

Your code is more clearly written using "continue;" in place of the
raw ";" in the while loop, and the judicious use of blanks to
separate items improves readability.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-18-2005
"bhanu" <> writes:
> Try this program: EOF is always -1, not any negative number.

[snip]

C99 7.19.1p3, describing the standard header <stdio.h>, says:

The macros are
[...]
EOF

which expands to an integer constant expression, with type int and a
negative value, that is returned by several functions to indicate
end-of-file, that is, no more input from a stream;
[...]

The behavior of the program you posted is consistent with this. An
implementation with EOF==-2 or EOF==-20000 would also be consistent
with the standard.

(I'm assuming that your program correctly demonstrates that EOF==-1
for a given implementation; I haven't actually confirmed this.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
pete <> writes:
> alice wrote:
>>
>> hi all,
>> What will happen when I tries to input a character from a file
>> which is finished,
>> in other words what will 'c' contains after the last
>> statement in the following code is executed :
>>
>> FILE * file =fopen("FILE1.c","r");
>> char c;
>> while((c=fgetc(file)) != EOF);
>> c=fgetc(file);
>> // what is value of c now???????????????????

>
> (char)EOF


If char is unsigned, the while() is an infinite loop (since c will
never have a negative value).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
CBFalconer <> writes:
> alice wrote:
>>
>> What will happen when I tries to input a character from a file
>> which is finished, in other words what will 'c' contains after
>> the last statement in the following code is executed :
>>
>> FILE * file =fopen("FILE1.c","r");
>> char c;
>> while((c=fgetc(file)) != EOF);
>> c=fgetc(file);
>> // what is value of c now???????????????????

>
> First of all c must be of type int, not char. EOF is outside the
> range of chars. With that change, your final c will _probably_ be
> EOF, since most disk files have what is called sticky eof. If the
> input file happens to be a terminal, it may not have sticky EOF and
> anything can happen.


EOF may be (and probably is) within the range of type char if char
happens to be signed, but that's beside the point. The value returned
by fgetc() is either an *unsigned* char converted to int, or EOF.

Figuring out what problems can occur when the result of fgetc() is
stored in a char is fairly complex (and is probably a good exercise
for an intermediate student). Fixing the code so you don't have to
worry about those problems in the first place is easy.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check what symbols are defined in a .o files? .a files? and.so files in linux? yinglcs@gmail.com C++ 3 01-18-2009 05:23 PM
??? I'm looking for an Online BackUp service for data files (not sysem files), anyone have + or - comments ?? Dudat... Computer Information 2 06-04-2006 05:00 AM
how i can extract text from the PDF files,power point files,Ms word files? crazyprakash Java 4 10-30-2005 10:17 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Help! Files, Files, and more Files ... Everywhere JeffS Digital Photography 22 09-19-2004 01:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57