How to use redirection in C for file input How to use redirection in C for file input c c

How to use redirection in C for file input


Using redirection sends the contents of the input file to stdin, so you need to read from stdin inside your code, so something like (error checking omitted for clarity)

#include <stdio.h>#define BUFFERSIZE 100int main (int argc, char *argv[]){    char buffer[BUFFERSIZE];    fgets(buffer, BUFFERSIZE , stdin);    printf("Read: %s", buffer);    return 0;}


1.) you close stdin then assign a different file handler to it2.) replace stdin with any other file handler using dup2 function you can achieve it