tonykang22 / study

0 stars 0 forks source link

[리눅스 개발환경] 08. 표준 I/O 라이브러리 #79

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

08. 표준 I/O 라이브러리

표준 I/O 라이브러리란?

I/O 시스템 콜 표준 I/O 라이브러리
저수준 (커널 수준) 고수준 (어플리케이션 수준)
리눅스 커널 지원 시스템 콜 ANSI C 표준 지원 라이브러리
기본 버퍼링
Linux의 경우 open(), read(), write() 등 시스템 콜 이용 구현
open() fopen()
close() fclose()
read() fread()
write() fwrite()



표준 I/O 라이브러리 개요

사용자 버퍼 입출력



fopen

#include <stdio.h>

FILE * fopen (const char *path, const char *mode);



fclose

#include <stdio.h>

int fclose (FILE *stream);



fdopen

#include <stdio.h>

FILE * fdopen (int fd, const char *mode);



fgetc / fputc

#include <stdio.h>

int fgetc (FILE *stream);


#include <stdio.h>

int fputc (int c, FILE *stream);


fgetc / fputc 실습

int main(int argc, char argv[]) { FILE fp; int c;

if (argc < 2) {
    printf("Usage: %s <file>\n", argv[0]);
    return -1; 
}

fp = fopen(argv[1], "r+"); 
if (!fp) {
    perror("fopen error: ");
    return -1; 
}

while ((c = fgetc(fp)) != EOF) {
    if (c == '!' || c == '@' || c == '#' || c == '$' ||
    c == '%' || c == '^' || c == '&' || c == '*' || 
    c == '-' || c == '_') {
        if (ungetc(c, fp) == EOF) { 
            perror("ungetc error: ");
            return -1; 
        }

    if (ungetc(c, fp) == EOF) { 
            perror("fputc error: ");
            return -1; 
        }
    }
}

if (fclose(fp) == EOF) {
    perror("fclose error: ");
    return -1;
}

return 0;

}


<br>

- 결과
<img width="700" alt="image" src="https://user-images.githubusercontent.com/86760744/180644402-90fdc465-ae70-4bfc-8c24-61bf4543bc93.png">

<br><br>

### fgets / fputs

``` c
#include <stdio.h>

char * fgets (char *str, int size, FILE *stream);


#include <stdio.h>

int fputs (const char *str, FILE *stream);



fread / fwrite

#include <stdio.h>

size_t fread (void *buf, size_t size, size_t nr, FILE *stream);


#include <stdio.h>

size_t fwrite (void *buf, size_t size, size_t nr, FILE *stream);



fread / fwrite 실습

define FILENAME "./data.file"

int main(void) { FILE readfile, writefile;

struct address_info { 
    unsigned int index;
    char name[32];
    char phone_number[16]; 
    char address[100];
};

struct address_info kim, who;

kim.index = 1;
sprintf(kim.name, "Kim"); 
sprintf(kim.phone_number, "010-1234-5678"); 
sprintf(kim.address, "Seoul");

writefile = fopen(FILENAME, "w"); 
if (!writefile) {
    perror("fopen error: ");
    return -1; 
}

if (!fwrite(&kim, sizeof(struct address_info), 1, writefile)) {
    perror("fwrite error: ");
    return -1; 
}

if (fclose(writefile)) { 
    perror("fclose error: ");
    return -1; 
}

readfile = fopen(FILENAME, "r"); 
if (!readfile) {
    perror("fopen error: ");
    return -1; 
}

if (!fread(&who, sizeof(struct address_info), 1, readfile)) { 
    perror("fread error: ");
    return -1; 
}

if (fclose(readfile)) { 
    perror("fclose error: ");
    return -1; 
}

printf("index: %d\n", who.index);
printf("name: %s\n", who.name);
printf("phone number: %s\n", who.phone_number); 
printf("address: %s\n", who.address);

return 0;

}



<br>

- 결과
<img width="500" alt="image" src="https://user-images.githubusercontent.com/86760744/180644649-5809966a-f3b5-477b-8bff-c519d4f5381a.png">