C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 09:54 PM
motoot motoot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 motoot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 28 sec
Reputation Power: 0
Undefined ungetc behavior

hi!

i'm new at c so i'm trying to understnad something. here it is:

main.c:

Code:
#include <stdio.h>

int main(void)
{
      FILE *fp;

      fp = fopen("test.txt", "r+");

      putchar(fgetc(fp));
      putchar(fgetc(fp));
      putchar(fgetc(fp));
      //putchar(fgetc(fp));
      
      ungetc('t', fp);
      ungetc('U', fp);
      ungetc('P', fp);
      
      putchar('\n');
      putchar(fgetc(fp));
      putchar(fgetc(fp));
      putchar(fgetc(fp));
}


test.txt:
Code:
gET


outputs as:
Code:
gET
PUt


i'm using fgetc to make sure the file position gets increment once evry call .

but when i uncomment the 4th putchar(fgetc(fp)), the output become like this:

Code:
gET
t


it's like after EOF was reached, we can effectiively call ungetc only once. that's it... i thoght you can do it at least 3 times, like what happened before we uncommented the putchar(fgetc(fp)).

why???

Reply With Quote
  #2  
Old December 11th, 2012, 12:05 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
Quote:
Originally Posted by man page
DESCRIPTION
ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed-back characters will be returned in reverse
order; only one pushback is guaranteed.

RETURN VALUE
ungetc() returns c on success, or EOF on error.


When you put the input stream into an EOF condition with the 4th fgetc, then you can only expect one pushback to work.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 11th, 2012, 12:29 AM
motoot motoot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 motoot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 28 sec
Reputation Power: 0
uhhh.. i guess this is sumthing that i cant do anything about...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Undefined ungetc behavior

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap