5.표준 파일 입출력

5-1 파일입출력 전체 구조

파일 만들기 : fopen()

파일 열기 : fopen()

파일 쓰기 : fwrite()

파일 읽기 : fread()

파일 커서 이동 : fseek()

파일 닫기 : fclose();

5-2 파일 열기와 닫기

#include <stdio.h>

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

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

FILE  *freopen  (const  char *path, const char *mode, FILE

*stream);

DESCRIPTION

The fopen function opens the file whose name is the string

pointed to by path and associates a stream with it.

The argument mode points to a string beginning with one of

the following sequences (Additional characters may  follow

these sequences.):

표 10.1 참조

5-3 2진 형태 읽기 쓰기

SYNOPSIS

#include <stdio.h>

size_t  fread(  void *ptr, size_t size, size_t nmemb, FILE

*stream);

size_t fwrite( const void *ptr, size_t size, size_t nmemb,

FILE *stream);

DESCRIPTION

The function fread reads nmemb elements of data, each size

bytes long, from the stream pointed to by stream,  storing

them at the location given by ptr.

5-4 텍스트 형태 읽기 쓰기

SYNOPSIS

#include <stdio.h>

int scanf( const char *format, ...);

int fscanf( FILE *stream, const char *format, ...);

int sscanf( const char *str, const char *format, ...);

#include <stdarg.h>

int vscanf( const char *format, va_list ap);

int vsscanf( const char *str, const char *format, va_list ap);

int vfscanf( FILE *stream, const char *format, va_list ap);

DESCRIPTION

The scanf family of functions scans input according  to  a

format  as  described below.  This format may contain con□

version specifiers; the results from such conversions,  if

any,  are stored through the pointer arguments.  The scanf

function reads input from the standard input stream stdin,

fscanf  reads  input  from  the stream pointer stream, and

sscanf reads its input from the character  string  pointed

to by str.

The vfscanf function is analogous to vfprintf(3) and reads

input from the stream  pointer  stream  using  a  variable

argument  list  of  pointers  (see  stdarg(3).  The vscanf

function scans a variable argument list from the  standard

input  and  the  vsscanf  function scans it from a string;

these are analogous to the vprintf and vsprintf  functions

respectively.

Each  successive pointer argument must correspond properly

with each successive conversion specifier (but  see  `sup□

pression' below).  All conversions are introduced by the %

(percent sign) character.  The format string may also con□

tain other characters.  White space (such as blanks, tabs,

or newlines) in the format  string  match  any  amount  of

white  space,  including  none,  in the input.  Everything

else matches only itself.  Scanning stops  when  an  input

character  does  not match such a format character.  Scan□

ning also stops when an input conversion  cannot  be  made.

SYNOPSIS

#include <stdio.h>

int printf(const char *format, ...);

int fprintf(FILE *stream, const char *format, ...);

int sprintf(char *str, const char *format, ...);

int snprintf(char *str, size_t size, const  char  *format,

...);

#include <stdarg.h>

int vprintf(const char *format, va_list ap);

int  vfprintf(FILE  *stream,  const  char *format, va_list

ap);

int vsprintf(char *str, const char *format, va_list ap);

int vsnprintf(char *str, size_t size, const char  *format,

va_list ap);

DESCRIPTION

The  functions in the printf family produce output accord□

ing to a format as described below. The  functions  printf

and  vprintf  write  output to stdout, the standard output

stream; fprintf and vfprintf write  output  to  the  given

output  stream;  sprintf, snprintf, vsprintf and vsnprintf

write to the character string str.

The functions vprintf, vfprintf, vsprintf,  vsnprintf  are

equivalent  to  the  functions  printf,  fprintf, sprintf,

snprintf, respectively, except that they are called with a

va_list  instead  of a variable number of arguments. These

functions do not call the va_end macro. Consequently,  the

value  of  ap is undefined after the call. The application

should call va_end(ap) itself afterwards.

These eight functions write the output under  the  control

of a format string that specifies how subsequent arguments

(or arguments accessed via  the  variable-length  argument

facilities of stdarg(3)) are converted for output.

5-5 문자열, 문자 읽기 쓰기

SYNOPSIS

#include <stdio.h>

int fgetc(FILE *stream);

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

int getc(FILE *stream);

int getchar(void);

char *gets(char *s);

int ungetc(int c, FILE *stream);

DESCRIPTION

fgetc() reads the next character from stream  and  returns

it  as  an  unsigned char cast to an int, or EOF on end of

file or error.

getc() is equivalent to fgetc()  except  that  it  may  be

implemented  as  a  macro which evaluates stream more than

once.

getchar() is equivalent to getc(stdin).

gets() reads a line from stdin into the buffer pointed  to

by  s  until either a terminating newline or EOF, which it

replaces with '\0'.  No check for buffer overrun  is  per□

formed (see BUGS below).

fgets()  reads  in  at  most one less than size characters

from stream and stores them into the buffer pointed to  by

s.  Reading stops after an EOF or a newline.  If a newline

is read, it is stored into the buffer.  A '\0'  is  stored

after the last character in the buffer.

ungetc()  pushes  c back to stream, cast to unsigned char,

where it is  available  for  subsequent  read  operations.

Pushed  -  back  characters  will  be  returned in reverse

order; only one pushback is guaranteed.

Calls to the functions described here can  be  mixed  with

each  other  and  with calls to other input functions from

the stdio library for the same input stream.

5-6 파일 커서 이동

SYNOPSIS

#include <stdio.h>

int fseek( FILE *stream, long offset, int whence);

long ftell( FILE *stream);

void rewind( FILE *stream);

int fgetpos( FILE *stream, fpos_t *pos);

int fsetpos( FILE *stream, fpos_t *pos);

DESCRIPTION

The fseek function sets the file  position  indicator  for

the  stream  pointed to by stream.  The new position, mea□

sured in bytes, is obtained by adding offset bytes to  the

position  specified  by  whence.   If  whence  is  set  to

SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to

the  start of the file, the current position indicator, or

end-of-file, respectively.  A successful call to the fseek

function  clears  the end-of-file indicator for the stream

and undoes any effects of the ungetc(3)  function  on  the

same stream.

The  ftell  function obtains the current value of the file

position indicator for the stream pointed to by stream.

The rewind function sets the file position  indicator  for

the  stream  pointed  to by stream to the beginning of the

file.  It is equivalent to:

(void)fseek(stream, 0L, SEEK_SET)

except that the error indicator for  the  stream  is  also

cleared (see clearerr(3)).

The fgetpos and fsetpos functions are alternate interfaces

equivalent  to  ftell  and  fseek  (with  whence  set   to

SEEK_SET),  setting  and  storing the current value of the

file offset into or from the object referenced by pos.  On

some  non-UNIX  systems  an fpos_t object may be a complex

object and these routines may be the only way to  portably

reposition a text stream.

5-7 파일 컨트롤

#include <stdio.h>

int remove(const char *pathname);

DESCRIPTION

remove  deletes  a  name  from  the  filesystem.  It calls

unlink for files, and rmdir for directories.

int rename(const char *oldpath, const char *newpath)

DESCRIPTION

파일 이름 바꾸기