Test/C

18.1 파일개방 -->파일로 쓰기

kiostory 2017. 8. 11. 11:48

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{

    char ch;

 FILE *fp, *ofp;


 fp=fopen("a.txt", "r");  //파일명과 사용할 권한
 ofp=fopen("b.txt", "w");    // 이미파일이 있으면 내용을 삭제하고 새로 만든다.주의

// w 대신 a를 쓰면 append !

 if(fp==NULL) exit(1);


    while(1)
    {
        ch=fgetc(fp);             // OS가 생성한 버퍼의 주소를 리턴

        if(ch==EOF) break;

        fputc(ch, ofp);    //b.txt 파일이 생성되는 시점
        printf("%c",ch);            //한자씩 출력 (current point 증가...)

    }

    fclose(fp);
    fclose(ofp);

 return 0;
}