CS156: Intro to C, Part I

Spring 2018

File IO

See this page as a slide show

CS156 FileIO: File I/O

File I/O

fopen

fclose

File I/O

Great—we know how to open & close the file. How do we actually, you know, read or write to it?

File I/O functions

These functions keep track of where they are in the file. The first time that you call fgets, it reads the first line. The second time you call it, it reads the second line, etc.

Use the handle!

Writing cat char-by-char

#include <stdio.h>
int main() {
    int c;            // An int, not a char.
    FILE *handle = fopen("data","r"); // open for reading
    // should test if fopen failed here
    while ((c = fgetc(handle)) != EOF)
        fputc(c, stdout);
    fclose(handle);
    return 0;
}
% c11 fgetscEx.c
% a.out
This is a test of the emergency broadcast system

fgetc and int

Writing cat line-by-line

#include <stdio.h>
int main() {
    char buf[80];
    FILE *handle = fopen("data","r"); // open for reading
    // should test if fopen failed here
    while (fgets(buf, sizeof(buf), handle) != NULL)
        fputs(buf, stdout);
    fclose(handle);
    return 0;
}
% c11 fgetscEx.c
% a.out
This is a test of the emergency broadcast system

Example

#include <stdio.h>
int main() { 
    char line[100];     // store a line here
    FILE *in, *out;     // FILE object pointers
    in = fopen("infile","r");    // open for reading
    out = fopen("outfile","w");  // open for writing
    // should test if fopen failed here

    // read lines from the file until there are no more
    // fgets returns NULL if no more lines in file
    while (fgets(line,sizeof(line),in) != NULL)
        fputs(line,out); // print lines to outfile

    fclose(in);
    fclose(out);
    return 0;
} 

Formatted File I/O

Example

    #include <stdio.h>
    int main() { 
        int a,b,c;
        FILE *in = fopen("infile","r");    // open for reading
        FILE *out = fopen("outfile","w");  // open for writing
        // should test if fopen failed here

        // read pairs of numbers from the file
        count = fscanf(in, "%d%d", &a, &b);
        while (count==2) {
            fprintf(out, "Sum: %d\n", a+b);
            count = fscanf(in, "%d%d", &a, &b);
        }

        fclose(in);
        fclose(out);
        return 0;
    } 

I/O Buffering

Special Handles

I/O Error Checking

FILE *handle = fopen(inFilename,"r");
if (handle == NULL) {
    fprintf(stderr, "can't open %s\n", inFilename);
    exit(2);
}

I/O Error Checking

    #include <stdio.h>
    int main() { 
        char line[100];
        FILE *in = fopen("innie", "r"); // open file to read
        if (in == NULL) {
            printf("Can't open input file \"innie\"\n");
            return 1;
        }
        FILE *out = fopen("outie", "w"); // open file to write
        if (out == NULL) {
            // Do NOT fclose(out), because it’s not open.
            fclose(in);
            printf("Can't open output file \"outie\"\n");
            return 2;
        } 
        // read lines until end of file
        while (fgets(line,sizeof(line),in) != NULL)
            fputs(line,out);     // write a line to outfile

        fclose(in);              // close the file
        fclose(out);             // close the file
        return 0;
    } 

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-23T14:28

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building