How to Use File Redirection in C
This article will explain several methods of how to use file redirection in C.
Use the <
Operator to Redirect Standard Input
File redirection is generally known as I/O
redirection on UNIX based systems, which allows the user to redefine where standard input comes from, or standard output goes. <
operator is used to change where the standard input comes from. This method can be useful for taking user input from the file contents and storing it in the program buffer. In this case, we utilize the fgets
function to read the file’s contents until a newline character is encountered. When a new line character is read, it’s also stored in the given buffer, and string terminating null byte is stored after that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define SIZE 1000
int main(int argc, char *argv[]) {
char buf[SIZE];
printf("Write input text: ");
fgets(buf, SIZE, stdin);
printf("%s\n", buf);
exit(EXIT_SUCCESS);
}
Sample Command:
./program < input.txt
Output:
The first line from the input.txt file
In the previous sample code, we allocated a buffer with a hardcoded value. Still, the input file provided by the user may need more memory to store and we should implement an adaptive method to fit the scenario. For this, we allocate dynamic memory using malloc
and pass it the total file size as the maximum number that can be needed to store the first line in the file. This method can waste too much memory if the given file is too big and the first line is simply short. However, it guarantees that the code will be able to read in the longest line as long as malloc
will not fail and system memory will not run out.
Notice that, we retrieve a file size using stat
system call, but it needs the input file’s pathname, which can’t be retrieved if not passe explicitly as a command-line argument. Mind though, every function call needs to be checked for a successful return to guarantee the fgets
does not try to write to the uninitialized memory region and cause the program to terminate with failure.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
char *buffer = NULL;
const char *filename = NULL;
struct stat sb;
if (argc != 2) {
printf("Usage: ./program filename < filename\n");
exit(EXIT_FAILURE);
}
filename = argv[1];
if (stat(filename, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
buffer = malloc(sb.st_size);
if (!buffer) {
perror("malloc");
exit(EXIT_FAILURE);
}
printf("Write input text: ");
if (fgets(buffer, sb.st_size, stdin) == NULL) {
perror("fgets");
exit(EXIT_FAILURE);
}
printf("%s\n", buffer);
exit(EXIT_SUCCESS);
}
Sample Command:
./program input.txt < input.txt
Output:
The first line from the input.txt file
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook