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
512
votes
16 answers

'printf' vs. 'cout' in C++

What is the difference between printf() and cout in C++?
hero
  • 5,161
  • 3
  • 16
  • 6
162
votes
8 answers

stdlib and colored output in C

I am making a simple application which requires colored output. How can I make my output colored like emacs and bash do? I don't care about Windows, as my application is only for UNIX systems.
user142019
121
votes
5 answers

GCC fatal error: stdio.h: No such file or directory

I'm trying to compile a program in C on OS X 10.9 with GCC 4.9 (experimental). For some reason, I'm getting the following error at compile time: gcc: fatal error: stdio.h: No such file or directory I then tried a simple Hello World…
Jules
  • 14,200
  • 13
  • 56
  • 101
77
votes
8 answers

Rerouting stdin and stdout from C

I want to reopen the stdin and stdout (and perhaps stderr while I'm at it) filehandles, so that future calls to printf() or putchar() or puts() will go to a file, and future calls to getc() and such will come from a file. 1) I don't want to…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
71
votes
1 answer

Code for printf function in C

Possible Duplicate: source code of c/c++ functions I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in ,…
Rainer Zufall
  • 719
  • 1
  • 6
  • 3
68
votes
15 answers

Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error

I am trying to build a solution on Visual Studio Community 2017, but I keep getting the error "Cannot open include file: 'stdio.h' ". I've read through several similar questions, but I still can't fix this problem. It looks like the stdio.h file is…
dahiana
  • 1,171
  • 2
  • 9
  • 11
63
votes
5 answers

How to send output to stderr?

One uses this to send output to stdout: println!("some output") I think there is no corresponding macro to do the same for stderr.
tshepang
  • 12,111
  • 21
  • 91
  • 136
51
votes
9 answers

How can I capture STDOUT to a string?

puts "hi" puts "bye" I want to store the STDOUT of the code so far (in this case hi \nbye into a variable say 'result' and print it ) puts result The reason I am doing this is I have integrate an R code into my Ruby code, output of which is given…
script_kiddie
  • 1,167
  • 4
  • 12
  • 24
49
votes
6 answers

How can you flush a write using a file descriptor?

It turns out this whole misunderstanding of the open() versus fopen() stems from a buggy I2C driver in the Linux 2.6.14 kernel on an ARM. Backporting a working bit bashed driver solved the root cause of the problem I was trying to address…
Jamie
  • 7,075
  • 12
  • 56
  • 86
45
votes
5 answers

Can a program call fflush() on the same FILE* concurrently?

Can anything bad happen (like undefined behavior, file corruption, etc.) if several threads concurrently call fflush() on the same FILE* variable? Clarification: I don't mean writing the file concurrently. I only mean flushing it concurrently. The…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
38
votes
4 answers

What exactly is the FILE keyword in C?

I've started learning some C as a hobby and have blindly used FILE as a declaration for file pointers for quite some time, and I've been wondering. Is this a keyword or special data type for C to handle files with? Does it contain a stream to the…
johnholtsdater
  • 383
  • 1
  • 3
  • 4
37
votes
2 answers

Find all substring's occurrences and locations

I'm writing a program to parse some data saved as text files. What I am trying to do is find the location of every needle in a haystack. I already can read the file in and determine the number of occurrences, but I am looking to find the index also.
Thomas Havlik
  • 1,378
  • 4
  • 12
  • 20
35
votes
2 answers

Is there a Windows equivalent to fdopen for HANDLEs?

In Unix, if you have a file descriptor (e.g. from a socket, pipe, or inherited from your parent process), you can open a buffered I/O FILE* stream on it with fdopen(3). Is there an equivalent on Windows for HANDLEs? If you have a HANDLE that was…
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
35
votes
13 answers

Which C I/O library should be used in C++ code?

In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to stdio, insisting that it's more portable. Is this really the case? What is better to use?
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
34
votes
6 answers

What will happen if '&' is not put in a 'scanf' statement?

I had gone to an interview in which I was asked the question: What do you think about the following? int i; scanf ("%d", i); printf ("i: %d\n", i); I responded: The program will compile successfully. It will print the number incorrectly but it…
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
1
2 3
72 73