Mereketh Mereketh - 4 years ago 104
C Question

C program doesn't open file passed as argument

I'm writing a program that takes 2 filenames as arguments, then it opens it and elaborates the data.
Problem is that it doesn't open the second file, for some reason.

int main(int argc, char *argv[]) {

FILE *fltr_file, *ig_file;

ig_file = fopen(argv[2], "r");
fltr_file = fopen(argv[3], "r");

points to NULL (0x0) when I run the program. It takes
as arguments, both files are present in the program's directory.

Answer Source

The argv[0] is the program name, the argv[1] is the first parameter.

If your program accepts only two arguments, you probably need indices 1 and 2:

ig_file = fopen(argv[1], "r");
fltr_file = fopen(argv[2], "r");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download