File I/O

Though nearly every program we’ve written to date has been over without a trace as soon as its final line has been executed, C is not limited to only programs that do this. Indeed, C has the capability of file I/O as a means of storing persistent data that exists after our programs have finished running and to read information from a file during the course of the program’s execution. In this section, you are introduced to some of the basic file I/O functions they have at their disposal through stdio.h, and are guided through the process of writing some powerful I/O programs, including replicating several Linux commands they may have been using throughout the course of CS50 AP.

  • Lecture

  • Shorts

  • Notes

  • Thought Questions

    • How do we “read from” and “write to” files? What are these functions called? (Be sure to consult CS50 Reference for additional information on how to correctly use them.) Consider drawing a diagram mapping the relationship between “infiles” and “outfiles.”
    • Whenever we open a file, what must we remember to do after we are done working with it? Why is this important?
    • What information do you think is bound up in the FILE data type?
    • How does the way we interact with a file vary by its type? For instance, what do we do with a Word document that we wouldn’t do with a JPEG.
  • Problems