Я хочу, чтобы моя программа C попросила пользователя ввести имя файла, который они хотят открыть, и распечатать содержимое этого файла на экране. Я работаю из учебника C и до сих пор имею следующий код. Но когда я его выполняю, это фактически не позволяет мне вводить имя файла. (Я получаю «нажмите любую кнопку, чтобы продолжить», я использую кодовые блоки)Открытие файла из аргументов командной строки в C
Что я здесь делаю неправильно?
#include <stdio.h>
int main (int argc, char *argv[])
{
printf("Enter the file name: \n");
//scanf
if (argc != 2) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf("usage: %s filename", argv[0]);
}
else
{
// We assume argv[1] is a filename to open
FILE *file = fopen(argv[1], "r");
/* fopen returns 0, the NULL pointer, on failure */
if (file == 0)
{
printf("Could not open file\n");
}
else
{
int x;
/* Read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ((x = fgetc(file)) != EOF)
{
printf("%c", x);
}
fclose(file);
}
}
return 0;
}
+1. Вот как задать вопрос о домашнем задании. «Я зашел так далеко и попал в блокпост», а не «Напиши эту программу для меня». –