You are on page 1of 13

File Handling

Mr Benabderrezak
Introduction

• In C, you can create, open, read, and write to files by:

1. Declaring a pointer of type FILE

2. Use the fopen() function


Introduction
Create File

• To create a file, you can use the w mode inside the fopen() function

• The w mode is used to write to a file. However, if the file does not exist, it will
create one
Write To File
Write To File

• As a result, when we open the file on our computer, it looks like this:
Append Content
Append Content

• As a result, when we open the file on our computer, it looks like this:
Read File

• To read from a file, you can use the r mode:


Read File
Read File

Note
• The fgets() function only reads the first line of the file.
• If you remember, there were two lines of text in filename.txt
• To read every line of the file, we use a while loop
Read File
Read File

Hello World!
Hi everybody!

You might also like