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

Massive fprintf speed difference without "-std=c99"

I had been struggling for weeks with a poor-performing translator I had written. On the following simple bechmark #include int main() { int x; char buf[2048]; FILE *test = fopen("test.out", "wb"); setvbuf(test, buf, _IOFBF,…
Dave
  • 10,964
  • 3
  • 32
  • 54
18
votes
4 answers

Why does forking my process cause the file to be read infinitely

I've boiled down my entire program to a short main that replicates the issue, so forgive me for it not making any sense. input.txt is a text file that has a couple lines of text in it. This boiled down program should print those lines. However, if…
lbenedetto
  • 2,022
  • 1
  • 21
  • 39
17
votes
4 answers

How to tell GDB to flush the stdio of the program being debugged

The stdio is usually buffered. When I hit a breakpoint and there's a printf before the breakpoint, the printed string may still be in the buffer and I can not see it. I know I can flush the stdio by adding some flush code in the program. Without…
Yorkwar
  • 1,204
  • 1
  • 11
  • 27
17
votes
7 answers

Win32 - read from stdin with timeout

I'm trying to do something which I think should be simple: do a blocking read from standard input, but timing out after a specified interval if no data is available. In the Unix world this would be simple with select() but that doesn't work in…
Andy
  • 10,412
  • 13
  • 70
  • 95
16
votes
3 answers

Capturing stdout/stderr with NDK

I am porting some existing C code to run on Android. This C code writes lots of output to stdout/stderr. I need to capture this output, either in a memory buffer or a file, so I can then send it by email or otherwise share it. How can I achieve…
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
16
votes
4 answers

stdio.h not standard in C++?

I know most compilers allow both: #include and #include But someone argued that is not actually C++ standard. Is that true?
vladutcornel
  • 173
  • 1
  • 1
  • 8
16
votes
6 answers

Java: how to abort a thread reading from System.in

I have a Java thread: class MyThread extends Thread { @Override public void run() { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String msg; try { while ((msg = stdin.readLine()) != null)…
pts
  • 80,836
  • 20
  • 110
  • 183
16
votes
5 answers

What are the rules of automatic stdout buffer flushing in C?

I'm just curious which conditions should be satisfied to flush stdout buffer automatically. First of all I was confused that this pseudo code doesn't print output every iteration: while (1) { printf("Any text"); sleep(1); } But if I add…
Andrii Zymohliad
  • 1,237
  • 1
  • 11
  • 17
16
votes
8 answers

What's the best way to return a random line in a text file using C?

What's the best way to return a random line in a text file using C? It has to use the standard I/O library () because it's for Nintendo DS homebrew. Clarifications: Using a header in the file to store the number of lines won't work for…
Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
16
votes
2 answers

Writing to a file or stdout in Rust

I'm learning Rust, and I'm somewhat stumped. I'm trying to give the user the option of writing output to stdout or to a supplied filename. I started with the example code that's given for using extra::getopts located here. From there, in the do_work…
Neil
  • 466
  • 1
  • 6
  • 15
15
votes
2 answers

Visual Studio 15 __imp___iob, __imp___pctype, __imp___mb_cur_max

I am trying to use a library compiled with mingw in visual studio. However, I get the following linker errors: error LNK2001: unresolved external symbol __imp___iob error LNK2019: unresolved external symbol __imp___pctype referenced in…
stack_tom
  • 950
  • 2
  • 9
  • 18
15
votes
3 answers

How to send input to a program through stdin in Rust

I am attempting to write a shell in Rust. One of the functions of a shell is being able to redirect input to a file, redirect a file to input, and pipe output of a program into another program. I am using the run::process_output functions in std…
zephyrthenoble
  • 1,377
  • 3
  • 13
  • 20
14
votes
2 answers

Is snprintf(NULL,0,...); behavior standardized?

On Linux it returns the number of characters that would be printed. Is this standardized behavior?
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
14
votes
4 answers

Speed and stability of libraries 'stdio' vs. 'iostream'

When I search on the Internet for the difference between these two libraries, everyone says that is the standard I/O library of C++ and is for C. My professor says that cin>> and cout<< are not good functions and if we use cin>>…
Sam379
  • 271
  • 2
  • 4
  • 13
14
votes
4 answers

ftell at a position past 2GB

On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99 standard, is this undefined behavior since ftell must return a long int (maximum value being 2**31-1)?
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
1 2
3
72 73