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
85
votes
6 answers

What is the equivalent to getch() & getche() in Linux?

I am not able to find the equivalent header file for conio.h in Linux. Is there any option for getch() & getche() function in Linux? I want to make a switch case base menu where the user will give his option just by pressing one key & process should…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
55
votes
3 answers

getc() vs fgetc() - What are the major differences?

Everywhere I see "it is practically identical", or something similar... From The GNU C Programming Tutorial : There is another function in the GNU C Library called fgetc. It is identical to getc in most respects, except that getc is usually…
Joe DF
  • 5,438
  • 6
  • 41
  • 63
35
votes
4 answers

Reading \r (carriage return) vs \n (newline) from console with getc?

I'm writing a function that basically waits for the user to hit "enter" and then does something. What I've found that works when testing is the below: #include int main() { int x = getc(stdin); if (x == '\n') { …
MCP
  • 4,436
  • 12
  • 44
  • 53
24
votes
2 answers

getc Vs getchar Vs Scanf for reading a character from stdin

Of the below three functions: getc getchar & scanf which is the best one for reading a character from stdin and why? Are there any known disadvantages or limitations for any of these functions which makes one better than the other?
Jay
  • 24,173
  • 25
  • 93
  • 141
7
votes
1 answer

Perl6: getc in raw mode

I am using the Perl 6 module Term::termios. #!/usr/bin/env perl6 use v6; use Term::termios; my $saved_termios := Term::termios.new(fd => 1).getattr; my $termios := Term::termios.new(fd =>…
sid_com
  • 24,137
  • 26
  • 96
  • 187
5
votes
3 answers

Is the getc and putc implementation of K&R right?

In the K&R, chapter 8, it has a custom implementation of the putc and getc functions. In the first definition of getc, if the parameter is stdin, according to the definition of _iob, the function will try to write in the address 0 because this is…
user804371
4
votes
5 answers

Calculate Character Frequency in Message using Perl

I am writing a Perl Script to find out the frequency of occurrence of characters in a message. Here is the logic I am following: Read one char at a time from the message using getc() and store it into an array. Run a for loop starting from index 0…
Neon Flash
  • 3,113
  • 12
  • 58
  • 96
4
votes
2 answers

Why is rewind() not working as expected in this simple program?

Why is the below program not printing the first character of the newly created text file ("E") as expected? It's a simple program and I tried to look at the issue from all aspects but couldn't find the reason. The text file is being created on my D…
Meathead
  • 493
  • 2
  • 6
  • 15
3
votes
4 answers

Ruby STDIN.getc does not read char on reception

Seems that Ruby IO#getc wait until receiving a \n before returning the chars. If you try running this script: STDOUT.sync = true STDIN.sync = true while data = STDIN.getc STDOUT.puts "Char arrived" end It will return one "Char arrived" per char…
Martinos
  • 2,116
  • 1
  • 21
  • 28
3
votes
1 answer

How can I diagnose problems with Perl's getc?

I am having this strange problems with the getc function. I use getc to get a character from a socket file handler. I need to simulate message exchanges between pc and a mobile device. For the first few messages, getc works fine. But for this one,…
alex
  • 275
  • 1
  • 7
  • 16
3
votes
2 answers

ftell returning incorrect value

I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file…
Walter Zydhek
  • 293
  • 4
  • 10
3
votes
3 answers

getc return value stored in a char variable

On this Wikipedia page there is a sample C program reading and printing first 5 bytes from a file: #include #include int main(void) { char buffer[5] = {0}; /* initialized to zeroes */ int i; FILE *fp =…
w.b
  • 11,026
  • 5
  • 30
  • 49
3
votes
0 answers

Is there a C++ version of getc_unlocked( )?

While it seems that, in theory, std::istream::get() could be fast, in fact it isn't. I believe that one reason is that some locking is done for every call to std::istream::get(), and that for single-threaded programs this isn't needed. Is there an…
BenRI
  • 724
  • 6
  • 17
3
votes
1 answer

fgetc is always returning value 1

there is the following function: void readAndPrint(FILE * f) { int c; while(c = fgetc(f) != EOF) { printf("%d", c); } } In the main() body I used the following code to use the above function: FILE * pFile; pFile=fopen…
Prz3m3k
  • 605
  • 6
  • 14
2
votes
5 answers

Different output content file copy in C

Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method. The correct output with for loop: I am the worst programmer in the world! :D And this is bla bla bla bla …
BugShotGG
  • 5,008
  • 8
  • 47
  • 63
1
2 3 4 5 6 7