Questions tagged [ungetc]

22 questions
6
votes
3 answers

ungetc: number of bytes of pushback

ungetc is only guaranteed to take one byte of pushback. On the other hand, I've tested it on Windows and Linux and it seems to work with two bytes. Are there any platforms (e.g. any current Unix systems) on which it actually only takes one byte?
rwallace
  • 31,405
  • 40
  • 123
  • 242
6
votes
1 answer

Why does ungetc fail on some characters?

ungetc() seems to fail on some characters. Here is a simple test program: #include int main(void) { int c; printf("Type a letter and the enter key: "); #define TRACE(x) printf("%s -> %d\n", #x, x) TRACE(c = getc(stdin)); …
chqrlie
  • 131,814
  • 10
  • 121
  • 189
3
votes
2 answers

Why does ungetc have a parameter to specify which character to push back?

In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream, sort of like reversing the effect of an equivalent call to fgetc. If that's the case, then what's the point…
AJ Tan
  • 193
  • 7
2
votes
3 answers

Is there a C++ version of ungetc?

Is there a C++ version of ungetc? That is, can I put a character back onto an istream?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2
votes
2 answers

confusion regrading ungetc function

I am not understanding how this program works? char c; int i; for(i=1;i<=5;i++) { scanf("%c",&c); printf("%c",c); ungetc(c,stdin); } The output of the program is- character which is entered first time is printed 5 times.…
Ravi
  • 612
  • 1
  • 6
  • 17
2
votes
6 answers

ungetc in Python

Some file read (readlines()) functions in Python copy the file contents to memory (as a list) I need to process a file that's too large to be copied in memory and as such need to use a file pointer (to access the file one byte at a time) -- as in…
user78706
2
votes
2 answers

fscanf and switch not working

I'm trying to read this .txt file: ( 1 2 ( 3 4 ( 5 with this code: #include int main() { FILE* f = fopen("teste.txt", "r"); int i; char j; while (feof(f) == 0){ fscanf(f, "%c", &j); switch (j) { case '(': …
Rafael Dias
  • 83
  • 2
  • 9
2
votes
3 answers

InputStreamReader.markSupported is false

I need to “un-read” characters from an InputStreamReader. For that purpose I wanted to use mark and reset but markSupported returns false for the InputStreamReader class, since it doesn’t maintain an internal buffer and/or queue of characters. I…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2
votes
2 answers

IO::Handle to get and unget unicode characters

I think I've run into a problem with Unicode and IO::Handle. It's very likely I'm doing something wrong. I want to get and unget individual unicode characters (not bytes) from an IO::Handle. But I'm getting a surprising…
Michael
  • 8,446
  • 4
  • 26
  • 36
1
vote
1 answer

error when mixing calls to fgetc, ungetc() and fputc() witht the same file

This code mix calls to putc() and ungetc() on the same file: #include int main(int argc, char *argv[]) { FILE *fp; int c; fp = fopen("filetest.txt", "r+"); int fno = fileno(fp); /* get and print first position '1'…
MrIo
  • 108
  • 1
  • 10
1
vote
1 answer

Pushing characters back to stdin in C

Say input stream (stdin) has "abc" on it. I want to push back, say, 3 '*' chars to stdin to get something like "***abc". I was trying to use ungetc() (I did ungetc('*', stdin)) for this but I realized that it guarantees only 1 character pushback,…
StaticESC
  • 79
  • 5
1
vote
2 answers

modify fflush() that guarantee calling ungetc() twice in a row in C

I'm a C beginner, I want to call ungetc() twice in a row although I know in regular C it is not permitted. Someone told me I can modify Fflush() to do this job, however I don't know how to do it. Here is my code, my Fflush only allow one ungetc(), I…
yuan wang
  • 27
  • 4
0
votes
0 answers

why was my version of ungetch() and getch() wrong?

i'm searching for a long time on net. but no use. please help me. /* getch and ungetch to handle EOF Character In all the ungetch and getch * functions written so far, the buf is declared as char buf[BUFSIZ]. * Changing this to int buf[BUFSIZ]…
neil guo
  • 3
  • 3
0
votes
0 answers

Using ungetc() to get the length of stdin for allocate memory, is it possible?

Here is the logic what I am trying to do; get input char with fgetc() in the variable named ch, check if not ch is EOF or equal to '\n', increase int variable named counter, then put the variable ch to stdin using ungetc() repeat process After…
kek
  • 23
  • 4
0
votes
1 answer

Finding empty line using fscanf

I'm supposed to read some variables named from "A" to "Z" and then evaluate them. The values in variables are matrices. This is example input: B=[5 2 4; 0 2 -1; 3 -5 -4] E=[-6 -5 -8; -1 -1 -10; 10 0 -7] R=[-1 -7 6; -2 9 -4; 6 -10 2] R+E+B I have…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
1
2