본문 바로가기

아카이브/C

파일 입출력 함수들

반응형

함수명

Parameters

return value

비고

FILE *fopen

 

 

const char *filename

const char *mode

 

 

NULL if failed in accessing

ptr to file if successful

 

 

 

Mode:

 - file position is set to beginning if "r" or "w" and

     but set to the end if "a"

 - if "a" or "w" and the file does not exist, it's created.

 - "+" is added to  indicate the update node (both

     reading and writing).

 - "b" is added to open a binary file

 - (ex: "rb+" to open binary file for both read & write)

 - : update mode 에서 read 뒤에 fseek, fsetpos,

     또는  rewind 가 온 뒤에야 write 가 올수 있음.

 - : update mode 에서 write 다음에는 , fflush,

       fseek, fsetpos, 또는  rewind 가 온 뒤에야

       read 가 올 수 있음.

char *fgets( )

char *line

int n

FILE *fp

맨처음 읽는 글자가 eof 이면 NULL,

다른 경우는 line

최대 n-1 글자를 읽어 line 에 넣음.

/n 읽으면 이것까지 넣고(중요) 그만 읽음.

eof 만나면 그만 읽음

맨뒤에 /0 넣음

char *gets ( )

char *s

못읽었으면 NULL,

읽었으면 s

eof newline 글자를 만날 때 까지 읽어서 s

넣음 (, newline 글자는 넣지 않음).

int fputs( )

const char *s

FILE *fp

수행성공이면 음이 아닌 정수,

실패면 EOF

s 의 모든 글자를 화일로 출력 (\n 은 안보냄)

(: puts \n 을 추가로 보냄.)

int fgetc( )

FILE *fp

eof를 만나면, EOF

else 읽은 글자

에러가 있으면 EOF is returned with error indicator

set.

int fscanf( )

FILE *fp

const char *string

......

if input stream is empty, EOF

else number of successful conversions is returned.

 

파일의 끝에 도달한 상황에서는 EOF 를 반환함

int fprintf( )

FILE *fp

const char *string

......

만약 에러가 일어나면, -1.

else 출력한 글자의 수

 

int fseek( )

FILE *fp

long offset

int place

0 if successful

nonzero if unsuccessful

화일 포인터를 파일 내의 특정 위치로 옮기는 작업을 수행한다.

offset place 에 대하여 상대적으로 몇 바이트 만큼 떨어진 위치로 옮길 것인지를 지정한다 (따라서 음수, 0, 양수 모두 가능함.)

place의 가능값: SEEK_SET, SEEK_CUR, SEEK_END

long ftell( )

FILE *fp

fp 가 현재 가리키고 있는 화일 상의 위치

현재의 위치 값을 어디에 저장하였다가 나중에 이 위치를 다시 access 할 수 있는 하는 가능성을 제공함.

size_t fread()

 

 

 

 

 

void *a_ptr

size_t el_size

size_t n

FILE *fp

 

 

 

number of elements successfully read

( n 보다 작은 값 if eof is reached)

파일에서 el_size * n 만큼의 데이타를 읽어서 메모리의 a_ptr 이 가리키는 메모리에 저장한다. 여기서 el_size 는 한 원소의 크기를 나타내며 n 은 이러한 원소 몇개를 읽을 지를 나타낸다.

 

: size_t stddef.h 에 선언되어 있음.

   (sizeof 함수가 생성하는 수 의 타입임.)

size_t fwrite()

void *a_ptr

size_t el_size

size_t n

FILE *fp

number of elements successfully written

( n 보다 작은 값 if error occurs )

a_ptr 이 가리키는 메모리에서 el_size * n 만큼의 데이타를 읽어서 화일포인터 fp 가 지정하는 화일의 현재 위치에 저장한다. 여기서 el_size 는 한 원소의 크기를 나타내며 n 은 이러한 원소 몇 개를 출력할 지를 나타낸다.

 

: size_t stddef.h 에 선언되어 있음.

   (sizeof 함수가 생성하는 수 의 타입임.)

 

반응형

'아카이브 > C' 카테고리의 다른 글

A* 알고리즘 코드(에러있음)  (0) 2015.11.01