Questions tagged [stdio]

This tag is for questions regarding "Standard I/O", i.e. I/O using the facilities in the C header or using the standard streams stdin, stdout, stderr.

The C standard header <stdio.h> defines facilities for using data streams via FILE objects and also declares the pre-defined standard streams, stdin, stdout and stderr.

The standard IO streams can be used from C in two ways:

  1. Using standard IO streams as implemented by the standard header <stdio.h> e.g. fprintf (stdout, "hello, world\n");
  2. Using the underlying file descriptors directly using the facilities in <unistd.h> e.g. write (STDOUT_FILENO, "hello, world\n", 13);. Note that this is not ISO C, but POSIX.
1090 questions
34
votes
3 answers

C equivalent to fstream's peek

I know in C++, you're able to peek at the next character by using: in.peek();. How would I go about this when trying to "peek" at the next character of a file in C?
Anthony
  • 343
  • 1
  • 3
  • 4
33
votes
3 answers

C equivalent of autoflush (flush stdout after each write)?

In Perl, I can type: $|++; and anything printed to STDOUT will be automatically fflush()ed. Is there an equivalent in C? In other words, is there some way I can tell stdio to automatically fflush stdout after every printf(), the way it…
raldi
  • 21,344
  • 33
  • 76
  • 86
31
votes
3 answers

How can I get an int from stdio in C?

I have significant trouble with this... printf("> "); int x = getchar(); printf("got the number: %d", scanf("%d", &x)); Output > 1234 got the number: 1
maček
  • 76,434
  • 37
  • 167
  • 198
31
votes
6 answers

Detecting EOF in C

I am using the following C code to take input from user until EOF occurs, but problem is this code is not working, it terminates after taking first input. What's wrong with this code? float input; printf("Input No: "); scanf("%f", &input); …
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
30
votes
3 answers

Why is fread reaching the EOF early?

I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file. The loop runs…
James Schek
  • 17,844
  • 7
  • 51
  • 64
25
votes
5 answers

Clang Error - stddef file not found?

After upgrading to Ubuntu 13.10 "Saucy", Clang now gives me the error message: clang -Wall -Werror -std=c99 -ggdb -O0 5.1.c -o 5.1 In file included from 5.1.c:1: /usr/include/stdio.h:33:11: fatal error: 'stddef.h' file not found # include…
haziz
  • 12,994
  • 16
  • 54
  • 75
24
votes
4 answers

What is the difference between getch() and getchar()?

What is the exact difference between the getch and getchar functions?
bubble
  • 3,408
  • 5
  • 29
  • 51
24
votes
1 answer

How to read / parse input in C? The FAQ

I have problems with my C program when I try to read / parse input. Help? This is a FAQ entry. StackOverflow has many questions related to reading input in C, with answers usually focussed on the specific problem of that particular user without…
DevSolar
  • 67,862
  • 21
  • 134
  • 209
24
votes
4 answers

What does '#include ' really do in a C program

I am new to c programming and I was coding some simple programs "Hello world" style. In all of these programs I put #include in the top but I am not sure what this means exactly. I googled it and I found that stdio.h is a file that has…
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
23
votes
8 answers

Reading a line using scanf() not good?

scanf(" %[^\n]",line); A friend of mine suggested that using fgets() to read a line as input would be a much better idea than using scanf() as in the statement above. Is he justified?
amulous
  • 704
  • 2
  • 6
  • 15
23
votes
1 answer

fputc vs putc in C

I was reading the C programming book by Kernighan and and came across putc and fputc. I didn't quite understand the difference between the two or when would one be used over the other. I found some posts on StackOverflow which dealt with this topic…
Vivek Rai
  • 890
  • 1
  • 9
  • 22
21
votes
3 answers

Does fprintf use malloc() under the hood?

I want a minimal o-damn-malloc-just-failed handler, which writes some info to a file (probably just standard error). I would prefer to use fprintf() rather than write(), but this will fail badly if fprintf() itself tries to malloc(). Is there some…
Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
21
votes
4 answers

popen implicitly declared even though #include is added

This is tiny snippet of my code. #include #include #include #include #include #include #include #include #include
Chris Allen
  • 653
  • 4
  • 9
  • 17
21
votes
2 answers

Open file by inode

Is it possible to open a file knowing its inode? ls -i /tmp/test/test.txt 529965 /tmp/test/test.txt I can provide path, inode (above 529965) and I am looking to get in return a file descriptor.
dev
  • 1,119
  • 1
  • 11
  • 34
21
votes
2 answers

Close a FILE pointer without closing the underlying file descriptor

By using fdopen(), fileno() it's possible to open streams with existing file descriptors. However the proper way to close a file, once you've opened it with a stream is to fclose() the FILE pointer. How can one close the stream, but retain the open…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
1
2
3
72 73