간단한 파일 입출력

Posted 2012. 8. 13. 22:04

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
     int state; //error 체크변수
     char* pState;
     char str[20];

     // 일기전용 파일개방
     FILE* pfile = fopen("log.txt", "rt");

if(pfile == NULL){
  printf("file open error\n");
 }

 //fscanf함수활용
 while(1){
  pState = fgets(str, sizeof(str), pfile);
  if(pState == NULL)
   break;
  fputs(str, stdout); 
 }
 //파일종결
 state = fclose(pfile);
 if(state != 0){
  printf("파일닫기실패\n");
 }
}