Programing - C/C Basic grammar
115 파일 읽기/쓰기 시 에러 검사하기(ferror)
junmoyo
2017. 12. 24. 13:00
개념 :
1. 파일을 읽거나 쓸 때 에러가 발생 했는지 유무를 검사합니다.
2. 원형 : int ferror(FILE *stream)
소스코드 : main.c
#include<stdio.h>
int main(void)
{
FILE *fp;
int ch;
fopen_s(&fp, "C:\\Users\\junmo\\Desktop\\300\\115\\file.txt", "w+");
if (fp == NULL)
{
puts("쓰기 모드 파일 생성 실패");
}
else
{
fprintf(fp, "%s", "this is text file");
fclose(fp);
}
fopen_s(&fp, "C:\\Users\\junmo\\Desktop\\300\\115\\file.txt", "r");
if (fp == NULL)
{
puts("읽기 모드 파일 생성 실패");
}
else
{
while (!feof(fp))
{
ch = fgetc(fp);
if (ferror(fp))
{
puts("파일 읽기 중 에러가 발생 하였습니다.");
}
else
{
printf("읽은 문자 : %c \n", ch);
}
}
fclose(fp);
}
system("pause");
return 0;
}
결과