LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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: 352

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: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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: 145

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: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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: 352

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: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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



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 03:16 PM.

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
Open Source Consulting | Domain Registration