How can I have the user input a text files into a c program?
Basically, instead of having a static file such as: fp = fopen("text.txt", "r");...
... how can I have the user input their own file?
How can I have the user input a text files into a c program?
Basically, instead of having a static file such as: fp = fopen("text.txt", "r");...
... how can I have the user input their own file?
Just prompt the user to enter the filename. Save it as a string and then use fopen. Read how to get a line of text FAQ.
Instead of using a literal string as your filename ("inputFile.txt"), have a char array of appropriate size (bigger is better than smaller), and allow them to enter their own filename, into it. scanf("%s, filename) or fgets(filename, sizeof(filename), stdin), will work.
then
Code:FILE *fp = fopen(filename, "r"); //and check that fp is not null: if(!fp) { printf("\nError opening the %s file\n", filename); //etc. }
I would suggest you to use command line arguments.
Code:int main(int argc , char *argv[]) { int *fp; fp = fopen(argv[1] , "r"); . . . . return 0; }