Reading in files from a command line in C

49 views Asked by At

so i'm pretty new to C and i've been trying to figure out how to read a text file in for reading as in the line below.

./atc < example_input_001.txt

I'm trying to print the lines out one by one but I don't know how to access the file.

So far I've just been reading in the file like this but that's only because I know the file name, I need the program to work for any file given in the command line.

FILE *open_ADSB_file = fopen("example_input_001.txt", "r");

    if (open_ADSB_file == NULL) {
        printf("No valid file\n");
        exit(1);
    }
1

There are 1 answers

2
LaikaN57 On

You are close. What you are using is called stdin. You can access stdin just like a file. You should replace your fopen call with something more like freopen(NULL, "r", stdin).

freopen() reference