• Interacting With Files

    Why is interacting with files important?:

    • files allow data to persist after the program has finished executing
    • all programs prior to now can only store data for the length of time the program is executing

    How to interact with files?:

  • Open and Close a File

    A simple example of opening and closing a file:

    This program opens and closes a file and displays a message if file was successfully or unsuccessfully opened

    open_close_file.c:

    NULL

    File Location

    • the file location parameter in fopen("data/test.txt", "r"); is relative to where the executable is ran from
    • the file location is not relative to where the open_close_file.c file is located
    • if pwd returns
      code/files
      and I run a program that opens fopen("data/test.txt", "r");, then the program is going to look for
      code/files/data/test.txt
  • Read From a File

    A simple example of reading a file:

    This program reads from a file and displays the file contents to the user:

    read_file.c:

    data/test.txt:

    Things to note:

    • fscanf behaves very similarly to scanf

    Another example of reading a file:

    This program reads from a file using a while loop and displays the file contents to the user:

    read_file_with_while.c:

    data/test.txt:

    Things to note:

    • EOF means "end of file"
    • EOF is just a macro constant for -1
    • you could just compare -1, but EOF is more descriptive
  • Writing to a File

    A simple example of writing to a file:

    This program creates a files and then writes to it:

    write_file.c:

    Things to note:

    • fprintf behaves very similarly to printf
  • Write a Program to Copy One File's Content to Another File

  • Assignment

    Homework Problem 23.1, at the end of this problem you should submit to Laulima:

  • Optional Assignments

    Optional assignments are not graded and does not need to be turned in. It is intended to give you more practice (if you want it) using the programming concepts discussed in lecture. Some optional assignments are intended to introduce related concepts (if you care about it) not discussed in this course to broaden your knowledge about programming in general.

    If the optional assignment is modifying a required assignment, be sure to leave your previous assignment work untouched by copying them to a new folder for the optional assignment and modify only the files in the new folder

    Update 23.1 to take into account for multiple "mama"s on a single line

    • Modify the program so it will count the following as 5 "Mama"s:
    • Mama, Mama you know I love you
      Mama, Mama you're the queen of my heart
      Your love is like tears from the stars
      Mama I just want you to know lovin' you is like food to my soul
      

    Update 23.1 to use regular expressions (aka regex)

    • regex is text parsing syntax that has been ported to many different programming languages
    • You can learn the regex syntax at regexone
    • C has a regex library that you can use for this optional assignment
    • If getting regex working in C is too much of a hassle, you can also just try regex for the above text parsing assignments at rubular. Rubular is the Ruby programming language's version of regex, and it will allow you to test regex capabilities that are common to all regex libraries in other languages.