본문 바로가기
HOME

LinuxSP - Standard I/O Library: 함수

by mingnei__ 2025. 4. 25.

fopen(3), freopen(3), fdopen(3)

#include <stdio.h>
FILE *fopen(constage char *pathname, const char *mode);
FILE *fdopen(int filedes, const char *mode);
FILE *freopen(const char *pathname, const char *mode, FILE *fp);
/* return: On success → file pointer / On error → NULL, errno is set */

struct FILE

typedef struct {
    int cnt;
    unsigned char *base;
    unsigned char *ptr;
    unsigned flag;
    
    int fd;
} FILE;
mode description open() flags
r Open text file for reading. The stream is positioned at the beginning of the file. O_RDONLY
r+ Open for reading and writing. The stream is positioned at the beginning of the file. O_RDWR
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. O_WRONLY | O_CREAT | O_TRUNC
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. O_RDWR | O_CREAT | O_TRUNC
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. O_WRONLY | O_CREAT | O_APPEND
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. Output is always appended to the end of the file. POSIX is silent on what the initial read position is when using this mode.  For glibc, the initial file position for reading is at the beginning of the file, but for Android/BSD/MacOS, the initial file position for reading is at the end of the file. O_RDWR | O_CREAT | O_APPEND

 

fclose(3), fcloseall(3)

#include <stdio.h>
int fclose(FILE *fp);
/* return: On success → 0 / On error → EOF, errno is set */

#define _GNU_SOURCE
#include <stdio.h>
int fcloseall(void);
/* return: On success → 0 / On error → EOF */

 

setbuf(3), setvbuf(3)

표준 입출력 라이브러리가 제공하는 버퍼링의 종류를 설정하는 라이브러리 함수(3)이다.

#include <stdio.h>
void setbuf(FILE *fp, char *buf);
int setvbuf(FILE *fp, char *buf, int mode, size_t size);
/*return: On success → 0 / On error → !0, errno is set */

버퍼링 종류 세 가지

  mode description
full-buffered _IOFBF 입출력이 버퍼 단위로 이루어짐
line-buffered _IOLBF 출력이 라인 단위로 버퍼링 됨
non-buffered _IONBF 버퍼를 사용하지 않게 됨    

 

fflush(3)

스트림에 대해 아직 기록되지 않은 데이터를 커널에 전달하는 라이브러리 함수(3)이다.

#include <stdio.h>
int fflush(FILE *fp);
/* return: On success → 0 / On error → EOF, errno is set */

 

getc(3), fgetc(3), getchar(3)

한 번에 하나의 문자를 읽는 라이브러리 함수(3)이다.

getc()와 fgetc()는 구현 방식에 차이가 있다.

#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
/* return: On success → next char / On error, end of file → EOF */

 

ferror(3), feof(3), clearerr(3)

#include <stdio.h>
int ferror(FILE *fp);
/* return: On success → 0 / On error → !0 */

int feof(FILE *fp);
/* return: true, false */

void cleanerr(FILE *fp);

 

ungetc(3)

#include <stdio.h>
ungetc(int c, FILE *fp);
/* return: On success → c / On error → EOF */

 

putc(3), fputc(3), putchar(3)

지정된 파일에 하나의 문자를 쓰는 라이브러리 함수(3)이다.

putc()와 fputc()는 구현 방식에 차이가 있다. (putc()는 매크로로 구현, fputc()는 함수로 구현됨)

 

fgets(3), gets(3)

줄 단위 입력에 사용되는 라이브러리 함수(3)이다.

#include <stdio.h>
char *fgets(char *buf, int n, FILE *fp);
char *gets(char *buf);
/* return: On success → buf / On error, end of file → NULL */

 

fputs(3), puts(3)

줄 단위 출력에 사용되는 라이브러리 함수(3)이다.

#include <stdio.h>
int fputs(const char *str, FILE *fp);
int puts(const char *str);
/* return: On success → ret >= 0 / On error → EOF */

 

fread(3), fwrite(3)

#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nobj, FILE *fp);
size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *fp);
/* return: 성공적으로 읽거나 쓴 객체들의 개수 */ 
/* nobj: number of objects 인듯 */

 

ftell(3), fseek(3), rewind(3)

#include <stdio.h>
long ftell(FILE *fp);
/* return: On success → current offset / On error → -1L, errno is set */

int fseek(FILE *fp, long offset, int whence);
/* return: On success → 0 / On error → !0 */

void rewind(FILE *fp);
/* fseek(fp, 0, SEEK_SET); 과 같은 기능 */

 

ftello(3), fseeko(3)

ftell(), fseek()과 다른 점은 반환값의 자료형

#include <stdio.h>
off_t ftello(FILE *fp);
/* return: On success → current offset / On error → (off_t)-1, errno is set */

int fseeko(FILE *fp, off_t offset, int whence);
/* return: On success → 0 / On error → !0 */

 

fgetpos(3), fsetpos(3)

#include <stdio.h>
int fgetpos(FILE *fp, fpos_t *pos);
/* return: On success → 0 / On error → !0 */

int fsetpos(FILE *stream, const fpos_t *pos);
/* return: On success → 0 / On error → ! 0, errno is set */

 

printf(3), fprintf(3), sprintf(3), snprintf(3)

scanf(3), fscanf(3), sscanf(3)

fileno(3)

tmpnam(3), tmpfile(3)

tempnam(3)

'HOME' 카테고리의 다른 글

[Linux] 커널 컴파일(build linux kernel) (1)  (1) 2025.05.08
LinuxSP - Files and Directories: 함수  (0) 2025.04.25
LinuxSP - Files and Directories: 개념  (0) 2025.04.25
LinuxSP - FILE I/O: 함수  (0) 2025.04.24
LinuxSP - FILE I/O: 개념  (0) 2025.04.24