0

I am reading a file character by character and comparing it to ascii values. At the moment I am specifying the file to read myself, but what if I wanted the user to input their own file for my program? example my code below uses fopen("myFile.txt","r");

but I would like the user to pick their own file and redirect into my program. ex. a.out < myFile.txt

 #include <stdio.h>
#include <stdlib.h>

int main(){

FILE *myFile = fopen("myFile.txt", "r");
double count=0;
char single;

while((single=fgetc(myFile))!= EOF){

    if(single=='a'|| single == 'A'){
        A++;
        count++;
    }

}

return 0; }

1 Answers1

0

You can read the file name from the user using fgets and then try to open it.

char str[MAX_LIMIT]; 
fgets(str, MAX_LIMIT, stdin);
FILE *myFile = fopen(str, "r");

Keep in mind that the file you are trying to open has to be in the same directory, if it's in a different location then user has the specify the full path to it.

adgerger
  • 41
  • 3