Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab 06 - File Handling

Reading from a file

We can read from a file in the following manner:

# include <stdio.h> //contains declarations for input/output functions
# include <string.h>

int main()
{
    FILE *pFile; //create a pointer to a file object

    char myBuffer[50]; //somewhere to store the data we read in

    pFile = fopen("people.txt", "r"); //opens a file called people.txt in READ mode
    if(pFile == NULL)
    {
        printf("Failed to open file\n"); //check if file failed to open
    }
    else
    {
        while(fgets(myString, 50, pFile) != NULL) //loop through file, reading 50 characters at a time
        {
            printf("%s", myString); //print them
        }
        fclose(pFile); //close file after finishing with it
    }

    return 0;
}

We open the file (in “r” (read) mode), and check that it has been opened properly. We use fgets to read data into a buffer. This will keep reading data into the buffer until one of the following conditions are met:

  • End of file
  • Buffer is full
  • End of line

In this case, we simply print the resulting string, and then close the file. As we are in a loop, we the printing will happen repeatedly for each line until the end.

Writing to a file

We can write to a file in the following manner:

# include <stdio.h>
# include <string.h>

int main()
{
    FILE *pFile;

    char myString[] = "my string goes here";

    pFile = fopen("myText.txt", "w"); //this time in WRITE mode
    if(pFile == NULL)
    {
        printf("Failed to open file\n");
    }
    else
    {
        fputs(myString, pFile); //fputs writes the string into the file
        fclose(pFile);
    }

    return 0;
}

We create a pointer to a file object, and open the file for writing. Note that the “w” parameter denotes that the file will be written to. If the file doesn't exist, it will be created. If it exists already, it will be overwritten.

Note that you can open the file in append mode “a” to write to the *end* of an existing file. Note that to simply modify a file, first read the whole thing, edit it in your code, then overwrite the previous file.

Tasks

This lab will be creating a contact system. Feel free to extend your existing (contact system) code.

Task 0.

Prepare a text file on your computer with the following contacts:

Miles Cannon
23
Leonardo Collier
31
George Byrd
22
Kaydence Best
39
Kaelyn Foster
21
Kelvin Frye
54
Maximus Wiley
48
Richard Molina
42
Angel Taylor
35

Representing people's names and then ages. Note you can modify this list if you want to have additional contact variables eg. date of birth.

Task 1.

Write a program which will open this file. It will read in the contact information for each person. It will create a contact struct for each person. It will allow the user to display all contact information.

Task 2.

Allow the user to add new contacts (you can use existing code) in your program. When the user prompts, save the new contacts to a text file. Check that when you close and reopen the program, as it reads from the file at the start, it maintains the contact list (they still exist even after closing and restarting the program.)

Extension tasks: Implement deleting of contacts. Allow the user to type in a name, find it in the list of contacts, and delete it.

courses/be5b99cpl/labs/lab06.txt · Last modified: 2023/11/01 09:44 by brouggeo