FF0001/3: 프로그래밍 I/II 이론dcslab.snu.ac.kr/courses/pp2020s/Lecture15.pdf · EOF •End...

12
File I/O

Transcript of FF0001/3: 프로그래밍 I/II 이론dcslab.snu.ac.kr/courses/pp2020s/Lecture15.pdf · EOF •End...

  • File I/O

  • File Open and Close

    • File Pointer • Pointer to FILE structure

    FILE *fp;

    FILE *fopen(char *name, char *mode);

    • Open a fileFILE *fp;

    fp = fopen("filename","r");

    • "r" for read, "w" for write, "a" for append.

    • Close a filefclose(fp);

  • FILE

    typedef struct {

    char *fpos; /* Current position of file pointer (absolute address) */

    void *base; /* Pointer to the base of the file */

    unsigned short handle; /* File handle or File descriptor*/

    short flags; /* Flags (see FileFlags) */

    short unget; /* 1-byte buffer for ungetc (b15=1 if non-empty) */

    unsigned long alloc; /* Number of currently allocated bytes for the file */

    unsigned short buffincrement; /* Number of bytes allocated at once */

    } FILE;

  • Character File I/O

    • Read a character from a filec=getc(fp);/* macro,EOF for end of file or error */

    c=fgetc(fp);/* function */

    int ungetc(int c, FILE *fp); /* push c back to fp */

    • Write a character to a fileputc(c,fp); /* macro */

    fputc(c,fp); /* function */

    • Check EOF or errorfeof(fp); /* non-zero if EOF */

    ferror(fp);

    /* non-zero if error occurred on the stream fp */

  • EOF

    • End Of File

    • Symbolic Constant, making nothing in a program depend on the specific numeric value

    • Defined in stdio.h

    • #define EOF -1or

    • #define EOF 0

  • Example: Copy file1 file2

    #include

    int main(int argc, char *argv[])

    {

    FILE *fp1, *fp2;

    int c;

    fp1 = fopen(argv[1],"r");

    fp2 = fopen(argv[2],"w");

    while((c=getc(fp1))!= EOF)

    putc(c,fp2);

    fclose(fp1);

    fclose(fp2);

    return 0;

    }

  • Formatted File I/O

    int fscanf(FILE *fp, char *format, ...)

    int fprintf(FILE *fp, char *format, ...)

  • Standard File I/O

    • OS opens three files and provides (constant) file pointers to them

    (declared in stdio.h). OS assigns them to a program.

    • stdin : standard input connected to the keyboard

    • stdout : standard output connected to the screen

    • stderr : standard error connected to the screen

    • OS is responsible for closing the three files.

    • You can close stdin and stdout if not needed. Reassigned by freopen.

    • Macros defined in terms of stdin and stdout

    #define getchar() getc(stdin)

    #define putchar(c) putc((c), stdout)

  • Error Handling – stderr and exit

    #include /* cat: concatenate files, version 2 */main(int argc, char *argv[]){

    FILE *fp;void filecopy(FILE *, FILE *);char *prog = argv[0]; /* program name for errors */if (argc == 1 ) /* no args; copy standard input */

    filecopy(stdin, stdout);else

    while (--argc > 0)if ((fp = fopen(*++argv, "r")) == NULL) {

    fprintf(stderr, "%s: can't open %s\n”, prog, *argv);exit(1);

    } else {filecopy(fp, stdout);fclose(fp);

    }

    if (ferror(stdout)) {fprintf(stderr, "%s: error writing stdout\n", prog);exit(2);

    }exit(0);

    }

    /* filecopy: copy file ifp to file ofp */

    void filecopy(FILE *ifp, FILE *ofp)

    {

    int c;

    while ((c = getc(ifp)) != EOF)

    putc(c, ofp);

    }

  • Error Handling – stderr and exit

    • exit calls fclose for each open output file to flush out any buffered output

    • Return values• 0: all is well

    • Non-zero : abnormal situation

  • Line Input and Output

    • char *fgets(char *line, int maxline, FILE *fp)

    /* fgets: get at most n chars from iop */char *fgets(char *s, int n, FILE *iop){

    register int c;register char *cs;cs = s;while (--n > 0 && (c = getc(iop)) != EOF)

    if ((*cs++ = c) == '\n’) break;*cs = '\0';

    return (c == EOF && cs == s) ? NULL : s;}

  • Line Input and Output

    • int *fputs(char *line, FILE *fp)

    /* fputs: put string s on file iop */int fputs(char *s, FILE *iop){

    int c;while (c = *s++)

    putc(c, iop);

    return ferror(iop) ? EOF : 0;}