473,657 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ungetc

av
Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
Jul 15 '06 #1
15 2414
av wrote:
Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
ungetc pushes characters back onto the stream, EOF isn't a character.
We already have fclose for closing a stream.
Jul 15 '06 #2
av wrote:
Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
EOF is not a character in any stream. Trying to 'push' it back onto the
stream doesn't make sense.

Given..
int ch;
ch = getc(pfile);
if (ch == EOF)
{
/* Assuming no error, getting here means the the previous call to
getc(pfile) was the last character in the stream.
*/
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 15 '06 #3
"Joe Wright" <jo********@com cast.netwrote in message
news:q-*************** *************** @comcast.com...
av wrote:
>Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?

EOF is not a character in any stream. Trying to 'push' it back onto the
stream doesn't make sense.

Given..
int ch;
ch = getc(pfile);
if (ch == EOF)
{
/* Assuming no error, getting here means the the previous call to
getc(pfile) was the last character in the stream.
*/
}
Well, it makes a little bit of sense. The C Standard requires that
ungetc accept EOF as a first argument (and return the failure code
EOF). That supports a "peek" idiom where you loop over a file first
peeking at a character (get/unget) then deciding whether to consume
it.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 15 '06 #4
av
On 15 Jul 2006 16:21:46 +0200, "Nils O. Selåsdal" wrote:
>av wrote:
>Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?

ungetc pushes characters back onto the stream, EOF isn't a character.
We already have fclose for closing a stream.
close in the way "pfile->flag|= _EOF;"
Jul 16 '06 #5
av
On Sun, 16 Jul 2006 06:26:10 +0200, av <av@ala.awrot e:
>On 15 Jul 2006 16:21:46 +0200, "Nils O. Selåsdal" wrote:
>>av wrote:
>>Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?

ungetc pushes characters back onto the stream, EOF isn't a character.
We already have fclose for closing a stream.

close in the way "pfile->flag|= _EOF;"
the above seems wrong
noone of you know if it is right "ungetc(EOF , pfile);"?
and for what it is intended to do? :)
Jul 17 '06 #6
"av" <av@ala.awrot e in message
news:4h******** *************** *********@4ax.c om...
On Sun, 16 Jul 2006 06:26:10 +0200, av <av@ala.awrot e:
>>On 15 Jul 2006 16:21:46 +0200, "Nils O. Selåsdal" wrote:
>>>av wrote:
Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?

ungetc pushes characters back onto the stream, EOF isn't a character.
We already have fclose for closing a stream.

close in the way "pfile->flag|= _EOF;"

the above seems wrong
noone of you know if it is right "ungetc(EOF , pfile);"?
and for what it is intended to do? :)
Two days ago I said:

Well, it makes a little bit of sense. The C Standard requires that
ungetc accept EOF as a first argument (and return the failure code
EOF). That supports a "peek" idiom where you loop over a file first
peeking at a character (get/unget) then deciding whether to consume
it.

Ergo, it is "right" to call ungetc with an EOF first argument
because the C Standard says what happens. It is useful to do so
in a loop where you get a character, immediately unget it, and
only then test to see if you got a character, as in:

while (ungetc(ch = fgetc(pfile), pfile) != EOF)
<do something with ch and maybe consume it>

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Jul 17 '06 #7
av
On Sat, 15 Jul 2006 10:57:25 -0400, Joe Wright
<jo********@com cast.netwrote:
>av wrote:
>Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?

EOF is not a character in any stream. Trying to 'push' it back onto the
stream doesn't make sense.

Given..
int ch;
ch = getc(pfile);
if (ch == EOF)
{
/* Assuming no error, getting here means the the previous call to
getc(pfile) was the last character in the stream.
*/
}
i read a float in the stream "2."
this is what i have to do (in the side stream only)

int ch1, ch2, ch3;
ch1=getc(stream ); // ch1='2'
ch2=getc(stream ); // ch2="."
ch3=getc(stream ); // ch3=EOF
ungetc(EOF);
ungetc(ch2);
because "." is not in the number so it seems i have to reput it in the
stream;
but the stream is eof so i have to erase eof
but in a std-input stream if i erase eof getc() continue to ask chars

it seems i have an easy soultion for above with a little change to
ungetc() code (ungetc(EOF, pf) if the stream is EOF set a bit in
pf->flag and unset _EOF bit. fillbuf see that bit and turn it in _eof)
Jul 18 '06 #8
In article <tl************ *************** *****@4ax.com>, av <av@ala.awrot e:
>int ch1, ch2, ch3;
ch1=getc(strea m); // ch1='2'
ch2=getc(strea m); // ch2="."
ch3=getc(strea m); // ch3=EOF
ungetc(EOF);
ungetc(ch2);
The C standard only promises a single character of push-back; any
more than that is a system-dependant extension.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Jul 18 '06 #9
On Tue, 18 Jul 2006 05:16:48 UTC, av <av@ala.awrot e:
>
int ch1, ch2, ch3;
ch1=getc(stream ); // ch1='2'
ch2=getc(stream ); // ch2="."
ch3=getc(stream ); // ch3=EOF
ungetc(EOF);
ungetc(ch2);
You can't unget more than one char.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jul 18 '06 #10

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

Similar topics

8
2934
by: Bill Cunningham | last post by:
Would getc and ungetc be the best and most simple what to parse expressions for a parser? Bill
11
3471
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 down it's pipe, but we can't send EOF down ungetc's pipe (especially when this pipe is the same)? ...
62
5007
by: Argento | last post by:
I was curious at the start about how ungetc() returns the character to the stream, so i did the following coding. Things work as expected except if I change the scanf("%c",&j) to scanf("%d",&j). I don't understand how could scanf() affect the content of i and i. Can someone tell me why? #include <stdio.h> #include <ctype.h> void main() {
0
8402
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8608
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2733
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
2
1627
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.