You mix command line arguments with user input.
When you use command line arguments, you execute the program and pass the arguments at the same time. For instance:
ShowContents MyFile.txt
In contrast, when you read user input, first run the program, and then specify the file name:
ShowContents Enter the file name: MyFile.Ttxt
Your program already reads the first argument to argv[1]
and treats it as the name of an open file. For the program to read user input, do the following:
char str[50] = {0}; scanf("Enter file name:%s", str);
Then the file name will be in str
instead of argv[1]
.
source share