I'm trying to write a program that will read the first character in a text file. If I use ./a.out myfile.txt
it works as intended but if I use ./a.out <myfile.txt
I get Segmentation fault: 11
. The reason why I'm trying to include the <
is because this what is in the spec of the assignment. The below code is just a simplified example that i've made that has the same issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int func(int argc, char **argv){
FILE *fp;
int test = 0;
fp = fopen(argv[1], "r");
fscanf(fp, "%i", &test);
printf( "current file: %s \n", argv[1]);
}
int main(int argc, char **argv){
func(argc, argv);
}
Is there any way I can get it to accept the argument as <myfile.txt
?