Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

problem with Data Files

 
 
Chris Torek
Guest
Posts: n/a
 
      01-19-2005
In article <(E-Mail Removed). com>
bhanu <(E-Mail Removed)> wrote:
>Try this program: EOF is always -1, not any negative number.

[most of program snipped; but here are the key parts]
>char c;
>while((c=fgetc(file)) != EOF)
>{
>printf("\nc = %d", c);
>}
>
>c=fgetc(file);
>printf("\n'c' After EOF = %d", c);


I tried this and got:

c = 255
c = 255
c = 255
c = 255
c = 255
c = 255
... [output repeats forever]

Can you guess why? (Hint: I did not use an Intel 80x86. In fact,
I ran this on a PowerPC system.)

C guarantees that EOF is "#define"d in <stdio.h> as a negative
integer, but -1 is not required, just typical. (The implementation
I used, on which this demonstration program failed, does in fact
define EOF as -1, so that was not the problem.)

C does not guarantee that plain "char" is signed, and in this case,
it is not.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
"bhanu" <(E-Mail Removed)> writes:
> Try this program: EOF is always -1, not any negative number.

[snip]

Something else I've thought of:

The standard says that EOF expands to an integer constant expression,
with type int and a negative value. It doesn't say or imply that its
value is -1. But I'd be (mildly) surprised if there were any existing
implementation that has a value other than -1 for EOF. For one thing,
it can make the implementation of the functions in <ctype.h> a little
easier.

But well-written code won't break on a hypothetical implementation
with EOF==INT_MIN.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-19-2005
Keith Thompson wrote:
> "bhanu" <(E-Mail Removed)> writes:
>
>> Try this program: EOF is always -1, not any negative number.

> [snip]
>
> Something else I've thought of:
>
> The standard says that EOF expands to an integer constant expression,
> with type int and a negative value. It doesn't say or imply that its
> value is -1. But I'd be (mildly) surprised if there were any existing
> implementation that has a value other than -1 for EOF. For one thing,
> it can make the implementation of the functions in <ctype.h> a little
> easier.
>
> But well-written code won't break on a hypothetical implementation
> with EOF==INT_MIN.


We should be able to confuse readers by replacing:

while (EOF != (ch = getchar()) {
/* process stuff */
}
with
while ((ch = getchar()) >= 0) {
/* process stuff */
}
or
do { /* much hairier, not input proof, assumes Ascii */
while ((ch = getchar()) > \'n') {
/* process stuff */
}
/* process eoln */
} while (ch >= 0);

--
"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
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      01-19-2005

Keith Thompson wrote:
> "bhanu" <(E-Mail Removed)> writes:
> > Try this program: EOF is always -1, not any negative number.

> [snip]
>
> Something else I've thought of:
>
> The standard says that EOF expands to an integer constant expression,
> with type int and a negative value. It doesn't say or imply that its
> value is -1. But I'd be (mildly) surprised if there were any

existing
> implementation that has a value other than -1 for EOF. For one

thing,
> it can make the implementation of the functions in <ctype.h> a little
> easier.
>


Why there's a requirement to expand EOF to an integer constant (with
type
int and negative value) ? If we see the ASCII character set, the max
value
is 0x7f. So, we can easily represent EOF to be -1 (0xff). Why does the
standard expand EOF to integer type.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
Jonathan Burd <(E-Mail Removed)> writes:
[...]
> Where did you lose the indentation?


The new version of groups.google.com is buggy.

I posted about this here recently, subject "groups.google.com
indentation bugs [semi-OT]".

<http://groups-beta.google.com/group/comp.lang.c/msg/3ebf130c8b967899?dmode=source>

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
(E-Mail Removed) writes:
> Keith Thompson wrote:
>> "bhanu" <(E-Mail Removed)> writes:
>> > Try this program: EOF is always -1, not any negative number.

>> [snip]
>>
>> Something else I've thought of:
>>
>> The standard says that EOF expands to an integer constant
>> expression, with type int and a negative value. It doesn't say or
>> imply that its value is -1. But I'd be (mildly) surprised if there
>> were any existing implementation that has a value other than -1 for
>> EOF. For one thing, it can make the implementation of the
>> functions in <ctype.h> a little easier.
>>

>
> Why there's a requirement to expand EOF to an integer constant (with
> type int and negative value) ? If we see the ASCII character set,
> the max value is 0x7f. So, we can easily represent EOF to be -1
> (0xff). Why does the standard expand EOF to integer type.


-1 is an integer constant with type int and negative value.

If you're suggesting that EOF could be a character value, that
wouldn't work. The standard doesn't assume the ASCII character set;
there are conforming C implementations with different character sets.
And if you're reading a binary file, 0xff is a valid input value.

EOF has to be distinct from all possible character values. This is
done by having fgetc() return an int that's either an *unsigned* char
value converted to int, or EOF.

(The standard probably could have required EOF to be -1, rather than
any arbitrary negative value, but it didn't.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
 
Eric Sosman
Guest
Posts: n/a
 
      01-19-2005


Keith Thompson wrote:
>
> The standard says that EOF expands to an integer constant expression,
> with type int and a negative value. It doesn't say or imply that its
> value is -1. But I'd be (mildly) surprised if there were any existing
> implementation that has a value other than -1 for EOF. For one thing,
> it can make the implementation of the functions in <ctype.h> a little
> easier.


An implementation that defined EOF as the value of
CHAR_MIN-1 (or even SCHAR_MIN-1) could allow the <ctype.h>
implementation to be more tolerant of a common error.
Whether such tolerance is a good idea is debatable, but an
implementor might well have the goal of running as many
programs as possible with as little trouble as possible,
whether those programs are faulty or not. Market pressures
sometimes favor convenience over strictness.

--
(E-Mail Removed)

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      01-19-2005
CBFalconer <(E-Mail Removed)> wrote:

> We should be able to confuse readers by replacing:
>
> while (EOF != (ch = getchar()) {
> /* process stuff */
> }
> with


> do { /* much hairier, not input proof, assumes Ascii */
> while ((ch = getchar()) > \'n') {


Assumes even more. It assumes no characters smaller than '\n' will
appear in the input. On ASCII systems on which '\n' equals 13, the page
feed character is smaller than it. I semi-regularly enter that character
at my console. I can also imagine people wanting to enter '\a', which on
an ASCII system is smaller than either option for '\n'.

Richard
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
(E-Mail Removed) (Richard Bos) writes:
> CBFalconer <(E-Mail Removed)> wrote:
>
>> We should be able to confuse readers by replacing:
>>
>> while (EOF != (ch = getchar()) {
>> /* process stuff */
>> }
>> with

>
>> do { /* much hairier, not input proof, assumes Ascii */
>> while ((ch = getchar()) > \'n') {

>
> Assumes even more. It assumes no characters smaller than '\n' will
> appear in the input. On ASCII systems on which '\n' equals 13, the page
> feed character is smaller than it. I semi-regularly enter that character
> at my console. I can also imagine people wanting to enter '\a', which on
> an ASCII system is smaller than either option for '\n'.


All of which, I think, is already covered by "not input proof".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
 
lawrence.jones@ugs.com
Guest
Posts: n/a
 
      01-19-2005
Lawrence Kirby <(E-Mail Removed)> wrote:
>
> 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.


The C Standard has always required it to be sticky. Some (perhaps even
many) implementations still get it wrong.

-Larry Jones

I don't need to do a better job. I need better P.R. on the job I DO.
-- Calvin
 
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