How to fputc 'EOF' to a FILE stream.

O

oksuresh

Hi talents,

I have a situation where , I should keep on reading a FILE stream until
a location. And I have to immediately write the EOF character , so that
the rest of the file is cleared.

Any idea?....

Thanks in Advance
Suresh
 
B

Ben Pfaff

I have a situation where , I should keep on reading a FILE stream until
a location. And I have to immediately write the EOF character , so that
the rest of the file is cleared.

This is a FAQ.

19.13: How can a file be shortened in-place without completely clearing
or rewriting it?

A: BSD systems provide ftruncate(), several others supply chsize(),
and a few may provide a (possibly undocumented) fcntl option
F_FREESP. Under MS-DOS, you can sometimes use write(fd, "", 0).
However, there is no portable solution, nor a way to delete
blocks at the beginning. See also question 19.14.
 
K

Keith Thompson

I have a situation where , I should keep on reading a FILE stream until
a location. And I have to immediately write the EOF character , so that
the rest of the file is cleared.

EOF is not a character. EOF is a macro that expands to a negative
constant that's used to indicate an end-of-file condition. (That
condition might be indicated by some character on some systems, but
that's system-specific, and is not related to EOF.)

See Ben Pfaff's answer.
 
C

CBFalconer

I have a situation where , I should keep on reading a FILE stream
until a location. And I have to immediately write the EOF character,
so that the rest of the file is cleared.

There is no EOF character. There is only an EOF condition of a
stream. The only portable way to handle your requirement is to
copy the file until you reach the stopping point. Then close both
the file and the new copy.

EOF is a negative int value, not a char value. It is distinct from
all characters. You don't know what it is, but you do know it is
defined in stdio.h. Streams return that value to signal that they
have encountered the end-of-file condition, i.e. there is no more
data.

--
"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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Richard Bos

CBFalconer said:
There is no EOF character.

This is not entirely true, which is probably much of the reason for this
confusion.
There _is_ an EOF character in ASCII (and possibly also in other
charsets), but it is only indirectly related to the C EOF macro, and
most definitely does not have the same value[1]. The two should not be
confused, but it's easy for a newbie to do so.

Richard

[1] Because all ASCII characters are zero-or-positive, while EOF < 0.
 
C

CBFalconer

Richard said:
CBFalconer said:
There is no EOF character.

This is not entirely true, which is probably much of the reason for
this confusion. There _is_ an EOF character in ASCII (and possibly
also in other charsets), but it is only indirectly related to the C
EOF macro, and most definitely does not have the same value[1]. The
two should not be confused, but it's easy for a newbie to do so.

No, there is NO EOF character, especially in ASCII. Some systems
assign some character, or character sequence, to signal EOF. Some
historically popular choices have been:

^Z SUB Who knows why, maybe because the last letter
^D EOT End of Transmission
^Y EM End of medium
^[ ESC Escape
^\ FS File Separator
^] GS Group Separator
^^ RS Record Separator
^_ US Unit Separator

The most commonly used sequences are: CR SUB and CR EOT as seen at
a keyboard. One system used ":EOF" at the immediate head of a line
only, i.e. preceded by CR.

Another generic means of signalling EOF from the keyboard is the
power switch. This one may have undesirable side effects.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
K

Keith Thompson

CBFalconer said:
There is no EOF character. There is only an EOF condition of a
stream. The only portable way to handle your requirement is to
copy the file until you reach the stopping point. Then close both
the file and the new copy.

EOF is a negative int value, not a char value. It is distinct from
all characters. You don't know what it is, but you do know it is
defined in stdio.h. Streams return that value to signal that they
have encountered the end-of-file condition, i.e. there is no more
data.

One more potential stumbling block: if char is signed, and EOF is,
say, -1, then EOF (or rather the value of what the EOF macro expands
to) *can* be a valid value of type char.

fgetc() and friends return either the value EOF, or a character value
*as an unsigned char converted to int*. A program that incorrectly
stores there result of fgetc() in a char object can mistakenly
interpret valid character data as EOF.
 
B

Barry Schwarz

Hi talents,

I have a situation where , I should keep on reading a FILE stream until
a location. And I have to immediately write the EOF character , so that
the rest of the file is cleared.

Any idea?....
C does not define an EOF character. Some systems define an EOF
character for certain uses (e.g., Windows uses 0x1a as an EOF
character in a text file, but not in a binary one). If your system
supports an EOF character, you would find out about it a newsgroup
that deals specifically with your system.


Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
C does not define an EOF character. Some systems define an EOF
character for certain uses (e.g., Windows uses 0x1a as an EOF
character in a text file, but not in a binary one). If your system
supports an EOF character, you would find out about it a newsgroup
that deals specifically with your system.

And even on such systems, writing an EOF character to a file may or
may not do anything useful.
 
R

Richard Bos

CBFalconer said:
Richard said:
CBFalconer said:
(e-mail address removed) wrote:

I have a situation where , I should keep on reading a FILE stream
until a location. And I have to immediately write the EOF character,
so that the rest of the file is cleared.

There is no EOF character.

This is not entirely true, which is probably much of the reason for
this confusion. There _is_ an EOF character in ASCII (and possibly
also in other charsets), but it is only indirectly related to the C
EOF macro, and most definitely does not have the same value[1]. The
two should not be confused, but it's easy for a newbie to do so.

No, there is NO EOF character, especially in ASCII.

*Goes to look this up*

By the noodly appendages of Cthulhu-in-a-dress, you're right! I never
realised this...

Mind you, it doesn't contradict my point, really; all it does is move me
more firmly into the newbie camp :-/

Richard
 
D

Dave Thompson

No, there is NO EOF character, especially in ASCII. Some systems
assign some character, or character sequence, to signal EOF. Some
historically popular choices have been:
Right.

^Z SUB Who knows why, maybe because the last letter
^D EOT End of Transmission
^Y EM End of medium

Agree; also ^C ETX End of Text. Especially in the little if ever used
ASCII-ized version of bisync, X3.20-something IIRC.
^[ ESC Escape

I've never seen any suggestion ESC was used by itself for EOF; it (and
to a somewhat lesser extent ^P DLE Data Link Escape) is very much used
as the prefix or introducer for a character or sequence representing
some function(s) according to many (mostly incompatible) schemes. I
could easily believe ESC-something for EOF, but not ESC alone.
^\ FS File Separator
^] GS Group Separator
^^ RS Record Separator
^_ US Unit Separator
FS or maybe GS _should_ have been used for EOF, but I've never seen it
happen. The latter two probably shouldn't.
The most commonly used sequences are: CR SUB and CR EOT as seen at
a keyboard. One system used ":EOF" at the immediate head of a line
only, i.e. preceded by CR.
Quite a few Unix (derived) programs, I believe also Multics, and
several Internet protocols perhaps inspired by them, use "period aka
dot aka full-stop alone on a line" e.g. CR . CR

In the (ITU) X.3 terminal=user interface, X.29 IIRC, you would use one
character to escape to command mode and then issue a disconnect
command. In Telenet (the now-gone company, not telnet the protocol) I
think this was return atsign D return. Similarly in most(?) telnet
clients today there is some command-escape followable by a command
conventionally 'quit'.

One system I use (sometimes) uses squiggle-letter as a way to enter
control-letter, so ~ Y means ^Y means EOF. You could count this as a
sequence or just as an encoded character.

X.25 supports grouping of packets using the 'M'ore bit, and SNA
supports grouping of RUs at two levels: chains and brackets. But these
are for arbitrary binary data not necessarily characters, and whether
such groups correspond to files or not depends on the application.
Another generic means of signalling EOF from the keyboard is the
power switch. This one may have undesirable side effects.

Or for a 'remote' terminal 'offline' or a modem/phone control.


- David.Thompson1 at worldnet.att.net
 
J

Jordan Abel

Quite a few Unix (derived) programs, I believe also Multics, and
several Internet protocols perhaps inspired by them, use "period aka
dot aka full-stop alone on a line" e.g. CR . CR

However, this is not a suitable representation for text files as defined
in the C standard. Unless perhaps an _actual_ period by itself on a line
is represented by CR . SPACE CR or something like that [preservation of
trailing whitespace being implementation-defined]
 
B

Ben Pfaff

Jordan Abel said:
Quite a few Unix (derived) programs, I believe also Multics, and
several Internet protocols perhaps inspired by them, use "period aka
dot aka full-stop alone on a line" e.g. CR . CR

However, this is not a suitable representation for text files as defined
in the C standard. Unless perhaps an _actual_ period by itself on a line
is represented by CR . SPACE CR or something like that [preservation of
trailing whitespace being implementation-defined]

Often these protocols escape lines that begin with a dot by
adding a second dot in front of it. The stream implementation
could do filtering on output to add it and on input to remove it.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top