fgetc, fputc 

 #include <stdio.h>
int main(void)
{
    FILE* stream;
    int file_state;   
    int input=0;

    stream=fopen("data1.txt", "w");
    if(stream==NULL)
        puts("파일 열기 에러");

    puts("데이터입력");
    while(input != EOF)
    {
        input=fgetc(stdin);
        fputc(input, stream);
    }
       
    file_state=fclose(stream);
    if(file_state==EOF)
        puts("파일 닫기 에러");
    return 0;
}

 

#include<stdio.h>
int main(void)
{
    FILE* stream1;    // 읽기 전용 파일 스트림 선언
    FILE* stream2;    // 쓰기 전용 파일 스트림 선언
    int input=0;

    stream1 = fopen("data1.txt","r");
    stream2 = fopen("data2.txt","w");

    puts("파일로부터 데이터를 입력");
    while( input != EOF )
    {
        input = fgetc(stream1);
        fputc(input, stream2);
        fputc(input, stdout);
    }
    fclose(stream1);
    fclose(stream2);
    return 0;
}

 

 

fflush

#include<stdio.h>
int main(void)
{
    int age;
    char name[20];
   
    printf("나이입력: ");
    scanf("%d",&age);

    //fflush(stdin); // 입력 버퍼를비운다.
   
    printf("이름을 입력: ");
    fgets(name, sizeof(name), stdin);

    printf("%d\n",age);
    printf("%s\n",name);
   
    return 0;
}

 

'프로그래밍언어 > C & CPP' 카테고리의 다른 글

프로그램 소스 코드  (0) 2014.09.27
struct Point 예제  (0) 2014.09.27
아규먼트  (0) 2014.09.13
두 행렬의 합, 차, 곱  (0) 2014.09.06
2차원 배열  (0) 2014.09.06

+ Recent posts