473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to fputc 'EOF' to a FILE stream.

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

Feb 15 '06 #1
12 11055
ok******@gmail. com writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Feb 15 '06 #2
ok******@gmail. com writes:
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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Feb 15 '06 #3
ok******@gmail. com 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. 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.c om, 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/googlegroupsrep ly/>
Feb 15 '06 #4
CBFalconer <cb********@yah oo.com> wrote:
ok******@gmail. com 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.

Richard

[1] Because all ASCII characters are zero-or-positive, while EOF < 0.
Feb 15 '06 #5
Richard Bos wrote:
CBFalconer <cb********@yah oo.com> wrote:
ok******@gmail. com 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. 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
Feb 15 '06 #6
CBFalconer <cb********@yah oo.com> writes:
ok******@gmail. com 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. 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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Feb 15 '06 #7
On 14 Feb 2006 21:40:46 -0800, ok******@gmail. com wrote:
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
Feb 16 '06 #8
Barry Schwarz <sc******@doezl .net> writes:
On 14 Feb 2006 21:40:46 -0800, ok******@gmail. com wrote:
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.


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

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Feb 16 '06 #9
CBFalconer <cb********@yah oo.com> wrote:
Richard Bos wrote:
CBFalconer <cb********@yah oo.com> wrote:
ok******@gmail. com 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
Feb 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2413
by: Mark A. Gibbs | last post by:
Good day, What is the safest way to get a non-end of file value? char_traits<char_type>::eof() will get me eof, but how can I safely and consistently generate a value that is not eof? should i use ~char_traits<char_type>::eof()? char_traits<char_type>::int_type()? char_traits<char_type>::eof() + 1?...
145
6180
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
11
3463
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an interactive terminal, it is probably nice to push EOF back (because to user doesn't want to provide an EOF twice). How is it getc can send EOF...
20
7880
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The standard says about fgets: synopsis #include <stdio.h> char *fgets(char *s, int n, FILE *stream);
6
5279
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem also when size was 2000 but line was 1200 bytes long. There is no such problem with .Net 2002 For .Net 2003 I used : #include <string> #include...
4
4308
by: Summu82 | last post by:
Hi i have to write some data in a binary file fputc(0x22,f2); this works fine also int data =0x22; fputc(0x22,f2);
8
17821
by: GiBo | last post by:
Hi! Classic situation - I have to process an input stream of unknown length until a I reach its end (EOF, End Of File). How do I check for EOF? The input stream can be anything from opened file through sys.stdin to a network socket. And it's binary and potentially huge (gigabytes), thus "for line in stream.readlines()" isn't really a way to...
6
6122
by: ericunfuk | last post by:
A basic question: When the documentation says "fseek() clears EOF indecator, does it mean when you seek over EOF, EOF is no longer at the original position and hence removed?Say, after I seek over the original EOF, when I fread() from a previous position that I know is before the EOF then fread will not be able to tell if it has encountered...
3
4304
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
When I execute the following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *bmp; bmp = fopen("deneme.bmp","w"); fputc(10,bmp);
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.