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
14
votes
4 answers

whitespace in the format string (scanf)

Consider the following code: #include int main() { int i=3, j=4; scanf("%d c %d",&i,&j); printf("%d %d",i,j); return 0; } It works if I give 2c3 or 2 c 3 or 2c 3 as input if I have to change the value of variables. What…
Vaibhav Agarwal
  • 1,124
  • 4
  • 16
  • 25
13
votes
3 answers

What does "< /dev/null >& /dev/null" at the end of a command do?

One of the scripts I run over ssh was hanging and I found a solution for it on this site: http://www.snailbook.com/faq/background-jobs.auto.html The site resolves the problem by adding this to the end of the command: xterm < /dev/null >& /dev/null…
user215997
  • 1,267
  • 2
  • 12
  • 12
13
votes
5 answers

C fgets versus fgetc for reading line

I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities: Use fgets and check each time if the last character is a newline and continuously append to a buffer Read each…
nc3b
  • 15,562
  • 5
  • 51
  • 63
13
votes
2 answers

String Stream in C

print2fp(const void *buffer, size_t size, FILE *stream) { if(fwrite(buffer, 1, size, stream) != size) return -1; return 0; } How to write the data into string stream instead of File stream?
Said
  • 133
  • 1
  • 2
  • 6
13
votes
2 answers

Where to find struct _IO_FILE

I was looking through /usr/include/stdio.h and happened to come across the following piece of code. /* Standard streams. */ extern struct _IO_FILE *stdin; /* Standard input stream. */ extern struct _IO_FILE *stdout; /* Standard…
ArunMKumar
  • 718
  • 2
  • 5
  • 14
12
votes
3 answers

Prevent inclusion of stdio.h (or other standard header)

A codebase I work with has historically tried--purposefully--to avoid dependencies on stdio.h creeping in. It has its own print formatting and mechanisms, and those are what's supposed to be used instead of printf etc. But someone adds a dependency…
12
votes
5 answers

Java + Eclipse: Synchronize stdout and stderr

I use Eclipse. When I have an application like this: write 20 times 'Hello World\n' to stdout write 'ERROR\n' to stderr write 5 times 'Hello World\n' to stdout The output looks many times like this: Hello World Hello World Hello World Hello…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
12
votes
8 answers

Unable to open a file with fopen()

I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code: #include #include #define CORRECT_PARAMETERS 3 int…
jet
  • 173
  • 1
  • 1
  • 6
11
votes
1 answer

printf() prints string without newline to stdout in line buffer mode after scanf()

I am aware that most terminals are by default in line buffer mode. i.e. output is buffered and not directed to stdout until a new-line character is encountered. So I would expect this to print nothing (at least before the buffer is filled up): int…
Chung Lun Yuen
  • 319
  • 4
  • 15
11
votes
3 answers

Why is buffered writing to a fmemopen()'ed FILE faster than unbuffered?

For sure, buffered I/O to a file on disk is faster than unbuffered. But why is there a benefit even when writing to a memory buffer? The following benchmark code example was compiled with gcc 5.40 using optimization option -O3, linked against glibc…
ralfg
  • 552
  • 2
  • 11
11
votes
2 answers

Is creating two FILEs for the same file descriptor well-defined?

POSIX specifies an fdopen function that creates a FILE for a file descriptor. POSIX also specifies a fileno function that returns the file descriptor for a FILE. Together, these two could be used to create a second FILE accessing the same underlying…
fuz
  • 88,405
  • 25
  • 200
  • 352
11
votes
5 answers

Redirecting stdio from a command in os.system() in Python

Usually I can change stdout in Python by changing the value of sys.stdout. However, this only seems to affect print statements. So, is there any way I can suppress the output (to the console), of a program that is run via the os.system() command…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
11
votes
3 answers

Is close/fclose on stdin guaranteed to be correct?

It seems as though the following calls do what you'd expect (close the stream and not allow any further input - anything waiting for input on the stream returns error), but is it guaranteed to be correct across all…
Greg Rogers
  • 35,641
  • 17
  • 67
  • 94
11
votes
2 answers

Does sprintf/snprintf allocate additional memory?

I'm writing a library and want to make it absolutely resource-agnostic which also means that library should live with user-supplied memory allocation functions. Library allows user to set their own error handler function too, which are called with…
firegurafiku
  • 3,017
  • 1
  • 28
  • 37
11
votes
3 answers

forcing a program to flush its standard output when redirected

i have a closed source program that prints output to standard output. i need to parse the output. so i redirect the output to a fifo (from which i can read in the parent process that forks and execs the binary) using dup2 and then exec the program.…
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191