The program reads the contents of the files specified as command line arguments. If the current argument causes an error (the file could not be opened), write an error message to the standard error output and continue execution with the following argument. The error message should be: File opening unsuccessful !.
#include <stdio.h>
int main() {
char name[1024];
scanf("%s",name);
FILE* fp = fopen("name.txt", "r");
if (fp !=0 ){
printf("Open is successfull");
} else {
printf("File opening unsuccessful! \n");
}
fclose(fp);
}
CodePudding user response:
You don't want to use scanf to read from stdin, you need to use command line arguments as listed in the question.
C - reading command line parameters
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "%s\n", "Incorrect number of arguments");
return 1;
}
FILE* fp = fopen(argv[1], "r");
if (fp !=0 ){
printf("File opening successful !\n");
fclose(fp);
} else {
fprintf(stderr, "%s\n", "File opening unsuccessful !");
}
}