How to use redirection in C for file input

前端 未结 2 724
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 14:42

I need to get the file from the terminal, I know the command will look like:

./a.out < fileName.txt

I\'m not sure how to use fgets() in

2条回答
  •  渐次进展
    2020-12-03 15:23

    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 
    
    #define BUFFERSIZE 100
    
    int main (int argc, char *argv[])
    {
        char buffer[BUFFERSIZE];
        fgets(buffer, BUFFERSIZE , stdin);
        printf("Read: %s", buffer);
        return 0;
    }
    

提交回复
热议问题