I/O Redirection
Standard I/O
A program processes input data and produces output. Input and output are abbreviated as I/O. Without other specified sources or destinations, they use standard input and standard output.
An open file has a numeric file descriptor (fd):
- Standard input, stdin (keyboard): fd = 0
- Standard output, stdout (display): fd = 1
- Standard error, stderr (display): fd = 2
Standard I/O
#include <stdio.h>
int main(int argc, char *argv[]) {
int num;
printf("(stdin) Enter an integer: ");
fflush(stdout);
if (fscanf(stdin, "%d", &num) != 1) {
fprintf(stderr, "Invalid input.\n");
return 1;
}
fprintf(stdout, "(stdout) num = %d\n", num);
fprintf(stderr, "(stderr) This is an error message.\n");
return 0;
}Output
(stdin) Enter an integer: 123
(stdout) num = 123
(stderr) This is an error message.File I/O
#include <stdio.h>
#include <stdlib.h>
void writeFile(const char *filename) {
FILE *fp = fopen(filename, "w");
if (!fp) {
fprintf(stderr, "File open failed.\n");
exit(1);
}
char *name = "Terry";
int age = 24;
double height = 179.8;
fprintf(fp, "Name: %s\nAge: %d\nHeight: %.2f\n",
name, age, height);
fclose(fp);
}
void readFile(const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "File open failed.\n");
exit(1);
}
char name[32];
int age;
double height;
if (fscanf(fp, "Name: %31s\nAge: %d\nHeight: %lf",
name, &age, &height) != 3) {
fprintf(stderr, "Invalid file data.\n");
fclose(fp);
exit(1);
}
printf("name: %s\n", name);
printf("age: %d\n", age);
printf("height: %.2f\n", height);
fclose(fp);
}
int main(int argc, char *argv[]) {
const char *filename = "info.txt";
writeFile(filename);
readFile(filename);
return 0;
}Output
name: Terry
age: 24
height: 179.80I/O Redirection
I/O redirection changes the default source or destination of standard streams. Standard input usually comes from the keyboard; reading from another source, such as a text file, is input redirection. Standard output usually goes to a terminal; sending it elsewhere, such as a file, is output redirection.
Output redirection uses >: create the file if it does not exist, or overwrite it if it does. With >>, create a missing file or append to an existing one. Standard error uses 2> and 2>>.
Input redirection uses <, but << does not mean appending input.
I/O Redirection
#include <stdio.h>
int main(int argc, char *argv[]) {
int data;
if (scanf("%d", &data) != 1) {
fprintf(stderr, "Invalid input.\n");
return 1;
}
printf("data = %d\n", data);
return 0;
}input.txt
12345Compile
gcc -Wall io_redirection.c -o io_redirectionRun
./io_redirection < input.txt > output.txtOutput: output.txt
data = 12345Route three I/O streams
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Separate input, results, and errors
Use input.txt, output.txt, and error.txt for I/O redirection.
- Compile io_redirection.c and put 12345 in input.txt.
- Read the input file, overwrite output.txt with stdout, and append stderr to error.txt.
- Inspect output.txt. Use the Lab to compare > and >> on repeated runs.