1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

int main(int argc, char* argv[]) {
    FILE* fp = fopen("g_weird.txt", "w");
    // FILE* is the type of a “file pointer”.
    // fp is often used as a variable name for a file pointer.
    // fopen(…) is a standard library function that opens a file.
    // We can operate on that file using fp.
    // "g_weird.txt" is the filename.
    // "w" is the file access mode.
    
    // In HW05, you did this:  fprintf(stderr, "Yo!");
    fprintf(fp, "Yo!");

    fputc('\0', fp);  // This is an odd thing to do.  You do NOT need to write the null
                      // terminator in a file.  I am doing this only to match the earlier
                      // example in this lecture.

    // Normally:  Do NOT write a null terminator in a file, unless you have a very special
    // reason.

    // RULE:  If you open a file, you must close it (once).
    fclose(fp);

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2020 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.