작성
·
304
0
아래와 같이 강의와 똑같이 코드를 작성하고 텍스트 파일도 Project1.vcxproj 파일과 같은 파일에 있는데 파일이 열리지 않습니다! 뭐를 잘못하고 있는지 알고 싶습니다!
코드
#define CRTSECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> //exit()
int main()
{
int c;
FILE* file = NULL;
char file_name[] = "my_file.txt";
file = fopen(file_name, "r");
if (file == NULL)
{
printf("Failed to open file.\n");
exit(1);
}
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
return 0;
}
출력
Failed to open file.
C:\Users\LG\Documents\Visual Studio 2022\Project1\x64\Debug\Project1.exe (process 18144) exited with code 1.
Press any key to close this window . . .
답변 1
0
(8.8 강의 코드가 맞나요?)
"my_file.txt"가 프로그램이 실행되는 동일한 폴더(디렉터리)에 생성되어 있는지를 먼저 확인해보세요.
프로그램이 파일을 찾을 위치를 정확히 알 수 있도록 파일 이름 대신 파일에 대한 절대 경로를 제공해 보십시오.
혹은 아래와 같이 전체 경로로 파일을 읽어들여보는 것도 좋습니다.
file_name[] = "C:\Users\LG\Documents\my_file.txt";