336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념 : 


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;

}




결과


+ Recent posts