LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2018, 09:10 PM   #1
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 313

Rep: Reputation: Disabled
Need FUNGETC


I believe GCC for C programs has ungetc which puts a character back in stdin and probably moves the pointer back 1. I need FUNGETC which would put a character back in in FILE * fp and move the pointer back 1. GCC does not seem to have this one. Anyone know how I would go about creating a function to do this? I might be able to get away with using fgetc in a program to preliminary get the string from fp and just move the pointer back for the big string when I form smaller strings. The reason I want this is to move the pointer back if I encounter valid data after getting a CR or LF (\r or \n) before the next valid read after I process the data I now have. Thank you. Alvin...
 
Old 01-04-2018, 11:04 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,482

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Just read the documentation of ungetc. (Note: This function is not specific to gcc or glibc.)
https://www.tutorialspoint.com/c_sta...ion_ungetc.htm

Note: you can unget only one character in each FILE. For seeking back, use functions fgetpos/fsetpos.

Last edited by NevemTeve; 01-04-2018 at 11:07 PM.
 
Old 01-05-2018, 06:43 AM   #3
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 132

Rep: Reputation: Disabled
Why do you need to put the character back, just fseek(file,-1L,SEEK_CUR)
 
Old 01-05-2018, 08:01 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,482

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
ungetc is more flexible as not all FILEs are seekable (pipes and sockets aren't)
 
Old 01-05-2018, 08:46 PM   #5
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 313

Original Poster
Rep: Reputation: Disabled
THanks GUYS for your help!
 
Old 01-06-2018, 07:28 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165
I was once writing a little recursive-descent compiler that needed this sort of thing, and it always needed to go back only-one character. So, I simply wrote that functionality into my homebrew getchar/ungetchar routines: "ungetchar" simply set a global boolean flag which caused "getchar" to un-set the flag and return the same character as before, instead of reading a new character from the source. Problem solved.

(As a bug-check, if "ungetchar" found the flag to be already-raised, it threw an exception.)
 
  


Reply

Tags
fungetc, ungetc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration