Files

Cards (14)

  • Why Files?
    • Input data can be lengthy and doing it several times (re-running the program) can be a hassle
    • Displaying output on the screen maybe convenient
    • But: when program terminates, output will be lost
    • Planning to execute the program many times
    • Program testing
  • Operations on a File
    • Declare a pointer to type FILE
    • This pointer will serve as the file handler which is used for accessing the file
  • Common operations
    • Opening an existing file: fopen()
    • Reading from file: fscanf()
    • Writing to a file: fprintf()
    • Closing a file: fclose()
  • Opening a File
    1. Create link between the program and the disk file
    2. Function for opening a file: fopen(filename, mode)
    3. Returns a pointer to type FILE
    4. filename: (string) name of the file to be opened
    5. mode: (string) specifies the mode for opening the file
  • Modes for fopen()

    • r: Opens an existing file for reading
    • w: Opens a file for writing. If the file doesn't exist, it is created. Writing of content is from the beginning of the file.
    • a: Opens a file for appending. If the file doesn't exist, it is created. If the file exists, the data to be written will be appended to the end of the file.
    • r+: Opens a file for both reading and writing
    • w+: Opens the file for both reading and writing. If the file doesn't exist, it is created. If the file exists, it's content will be emptied.
    • a+: Opens a file for both reading and appending. If the file doesn't exist, it is created. If the file exists, the data to be written will be appended to the end of the file.
  • fopen() returns NULL if an error occurs
  • Opening a File
    Whenever you attempt to open a file, check if a NULL is returned or not
  • Sample code for opening a file
    • FILE *file;
    file = fopen("filename.txt", "r");
    if (file != NULL) {
    /* Do what you want with the file */
    }
    else {
    /* Failed to open the file */
    /* Do what you want with this error */
    }
  • Reading from a File
    Reading file content using fscanf(file_pointer, format_string, loc_list);
    file_pointer: the pointer to the file opened
    format_string: specifies the format of the content to read from the file
    loc_list: addresses (of variables) to where the content read will be stored
  • Sample code for reading from a file
    • FILE *file;
    char str[50];
    int num;
    file = fopen("filename.txt", "r");
    fscanf(file, "%s %d", str, &num);
  • Writing to a File
    Writing content to a file using fprintf(file_pointer, format_string, src_list);
    file_pointer: the pointer to the file opened
    format_string: specifies the format to use for writing the content to the file
    arg_list: source(s) of data to be written to the file
  • Sample code for writing to a file
    • FILE *file;
    file = fopen("filename.txt", "w");
    fprintf(file, "%s %d %s", "AMAT", 152, "\nYes!");
  • Closing a File
    After every successful file operation, you must always close a file
    C automatically closes all opened files after program execution
    During program execution, better to close opened files to avoid any unintended modification to the content
    Closing a file using fclose(file_pointer);
    file_pointer: the pointer connected to the file
  • Sample code for closing a file
    • FILE *file;
    file = fopen("filename.txt", "r");
    /* After performing whatever you want with the file */
    fclose(file);