Lec6 - Files
Lec6 - Files
Angelika S. Balagot
Instructor
Target Topics
• Topic 5.1: What are Files?
• Topic 5.2: Types of Files
• Topic 5.3: File Operations
• Topic 5.4: File Handling
- Topic 5.4.1: Create a File
- Topic 5.4.2: Closing File
- Topic 5.4.3: Write to a File
- Topic 5.4.4: Append Content to a File
- Topic 5.4.5: Reading Files
• Topic 5.5: Error Handling
• Topic 5.6: Additional Information
Topic 5.1: What are Files?
What are Files?
• If you have to enter a large number of data, it will take a lot of time to
enter them all. However, if you have a file containing all the data, you
can easily access the contents of the file using few commands in C.
• You can easily move your data from one computer to another without
any changes.
Topic 5.2: Types of Files
Types of Files
Text files
• Text files are the normal .txt files that you can easily create
using Notepad or any simple text editors.
• When you open those files, you'll see all the contents within the
file as plain text. You can easily read, edit or delete the contents.
Binary files
• Binary files are mostly the .bin files in your computer. Instead of
storing data in plain text, they store it in the binary form (0's and
1’s).
• They can hold higher amount of data, are not readable easily
and provides a better security than text files.
Topic 5.3: File Operations
File Operations
FILE *fptr;
fptr = fopen(filename, mode);
• To actually open a file, use the fopen() function, which takes two
parameters:
Parameter Description
filename The name of the actual file user want to open.
For example: file.txt
• To create a file, user can use the “w” mode inside the fopen() function.
• This mode is used to write to a file. However, if the file does not exist, it
will create one.
FILE *fptr;
• Note: The file is created in the same directory as the user’s other C files,
only if nothing else is specified.
Create a File
• If you want to create a file in a specific folder, just provide the absolute
path of the file.
• The fclose() function will close the file when the work is done with it.
FILE *fptr;
• This mode means that the file is opened for writing. To insert content to it, you
can use the fprintf() function and add the pointer variable and some text.
fptr – sample pointer variable
FILE *fptr;
fptr = fopen (“file.txt”, “w”);
fprintf(fptr, “Sample Text”);
fclose(fptr);
Write to a File
• As a result, when we open the file on our computer, it looks like this:
FILE *fptr;
fptr = fopen (“file.txt”, ”w”);
fprintf(fptr, “Sample Text”);
fclose(fptr);
Write to a File
• Note: If you write to a file that already exists, the old content is deleted, and
the new content will be inserted:
FILE *fptr;
fptr = fopen (“file.txt”, ”w”);
fprintf(fptr, “Hello World”);
fclose(fptr);
Topic 5.4.4: Append Content to a File
Append Content to a File
• If you want to add contents to a file without deleting the old content, you can
use the “a” mode.
• This mode appends content at the end of the file;
FILE *fptr;
fptr = fopen (“file.txt”, ”a”);
fprintf(fptr, “\nSample Text”);
fclose(fptr);
• The 1st parameter specifies where to store the file content, which will be in
the string array.
• The 2nd parameter specifies the maximum size of data to read, which
should match the size of string(100).
• The 3rd parameter requires a file pointer that is used to read the file (fptr).
Read Files
• Note: If you remember, there were two lines of text in file.txt file. The
fgets function only reads the first line of the file.
Read Files
• To read every line of the file, you can use a while loop.
FILE *fptr;
char string[100];
fclose(fptr);
Topic 5.5: Error Handling
Error Handling
• If you try to open a file for reading that does not exist, the fopen() function
will return NULL.
• As a good practice, we can use the if statement to test for NULL, and print
some text.
FILE *fptr;
if (fptr == NULL){
printf(“The file does not exist!”);
}
fclose(fptr);
Error Handling
• If the file exist, read the content and print it. If the file does not exist, print a
message:
FILE *fptr;
if (fptr != NULL){
while(fgets(string, 100, fptr)){
printf(“%s”, string);
}
} else {
printf(“The file does not exist!”);
}
fclose(fptr);
Topic 5.6: Additional Information
Additional Information
Mode Description
“r” It opens an existing file for reading only.
It opens a new file for writing. If the filename does not exist it will be created and if the file
“w”
already exists then its contents are deleted.
“a” It appends the existing file. If the filename does not exist it will be created.
It opens an existing file for reading and writing. It indicates that the file is to be read before
“r+”
writing.
It opens a new file for reading and writing. If a file with the current filename exists then it is
“w+”
destroyed and a new file name is created.
It opens an existing file for reading and appending. Its stream is positioned at the end of the
“a+”
file content.
Additional Information
• Following are the list of functions used for reading a file:
fputs() int fputs(const char *str, FILE *stream); It is used for writing a line to a file.