Questions tagged [getc]

Anything related to C or C++ standard library functions `getc` (C) or `std::getc` (C++). These functions are used to read a single character from a stream.

Anything related to C or C++ standard library functions getc (defined in <stdio.h> C standard header) or std::getc (defined in <cstdio> C++ standard header). These functions are used to read a single character from a stream and they can also be implemented as C-preprocessor macros.

See CPPreference.com:

91 questions
1
vote
2 answers

Replacing getchar with fgetc or getc in while(getchar() != '\n');

People usually mention while( getchar() != '\n' ) when it comes to clearing the input buffer or overflow. fgetc or getc also work. For example: while( fgetc(stdin) != '\n' ) and while( getc(stdin) != '\n' ) Is there any particular reason why…
GabesGates
  • 13
  • 3
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
1
vote
1 answer

How can I have getch() just in some cases in C?

I don't really know how to type the Title so I came up with this. So basically I am trying to make some sort of a snake in C. But I don't know how to make the controls. Lets say you have pressed left arrow and the snake has to move left till another…
Boris Mutafov
  • 297
  • 3
  • 7
1
vote
1 answer

Searching for a token in a file and copying each character after it into a string until another token is found

I am writing a program which seaches a file and each time it comes across the '<' charachter, it copies it and each following character into a string until a '>' is reached. So far this is what I've done: while(!file.eof()){ char c; string…
KOB
  • 4,084
  • 9
  • 44
  • 88
1
vote
5 answers

read multidigit int from file c

So I have a text file called num.txt that has a string of integers separated by a space. So let's say num.txt contains: 5 3 21 64 2 5 86 52 3 I want to open the file in read format and get the numbers. So I can say int iochar; FILE *fp; fp =…
truffle
  • 455
  • 1
  • 9
  • 17
1
vote
2 answers

How to read two consecutive characters from a file in C?

I have the following code, where I want to create a simply scanner for a simple calculator language. I am using fgetc to get the character from the file. Though, at some places I also need to check the next character that is followed. For this…
user1726549
1
vote
1 answer

difference between getc and fscanf

why does the following code work fine: #include int main() { FILE *fp=fopen("input.txt","r+"); char c; while((c=getc(fp))!=EOF) { printf("%c",c); } fclose(fp); return 0; } but this code gives an…
Nitin Labhishetty
  • 1,290
  • 2
  • 21
  • 41
1
vote
0 answers

Read multiple lines

in one of my C projects I need to read multiple lines. Let's say I expect some list of instructions, so the input might look like this (of course I don't know the maximal length): 1. First do this... 2. After that... ... n. Finish doing this... I…
quapka
  • 2,799
  • 4
  • 21
  • 35
1
vote
2 answers

How can "an expression with side effects" be passed to getc?

"Advanced Programming in the UNIX Environment, 3rd Edition", page 151: The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things: The argument to…
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
1
vote
2 answers

how to make getc() on stdin be binary

I'm using a while loop with getc() to read from a file handle and it works fine. Now I'm adding support for pipes. My problem is that while x0A, X0D, and x0A0D pass just fine, any cases of x0D0A get reduced to just x0A. Also x1A seems to stop the…
1
vote
1 answer

What does it mean, when Term::ReadKey::ReadKey returns "0"?

With dumpkeys --long-info called in a Linux-Terminal I get these values: # ... 0x0000 nul 0x0001 Control_a 0x0002 Control_b 0x0003 Control_c 0x0004 Control_d # ... When I run this script and press Ctrl a or Ctrl b I get the corresponding…
sid_com
  • 24,137
  • 26
  • 96
  • 187
1
vote
1 answer

fgetc,getc causes the program to crash when reading from text file

Whats wrong? I'm pretty sure my syntax is correct since it has no warnings. Plus it won't go past getc(document); I tried fgetc(document); same result. What am I not getting here? (I used printf("$"); to see where it crashes) char temp[51]; int cntr…
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
0
votes
4 answers

getc(fp) causing trouble

Here is my code. #include #include int main(int argc,char** argv) { char a; a=9; FILE * fp; fp=fopen(argv[1],"r"); while(a!= EOF) { a=fgetc(fp); printf("\n%d",a); } } The output to this…
Kraken
  • 23,393
  • 37
  • 102
  • 162
0
votes
3 answers

Why is my program using Perl's getc function not working properly?

I want to calculate the frequency of occurrence of chars in a message using Perl. For instance, if the char "a" appears 10 times in a message, then the frequency would be 10. To do this, I am reading the message from a FILE one char at a time using…
Neon Flash
  • 3,113
  • 12
  • 58
  • 96
0
votes
1 answer

How Can I Get getc() to Return a Character Besides a Smiley Face?

I am trying to write a simple 'cat' clone in C. I'm running Windows 7 and using the MinGW compiler. However, whenever I run the program, it returns the text file but with each character replaced with a '☺' character. Thank you in advance. #include…
pstap92
  • 191
  • 1
  • 1
  • 4