Declaring, Opening & Closing File Streams in C Programming

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Reading & Writing to Text Files in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Handling File Streams in C
  • 0:25 Declaring a File Pointer
  • 0:45 Opening, Creating, Closing
  • 3:05 Writing and Reading
  • 4:43 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami

Meghalee has a masters of computer science and communication engineering.

This lesson explains handling file operations, like opening and closing a file, in C programming. You will also learn how to perform read and write operations on a file. Various examples are shown to understand file streams.

Handling File Streams in C

A file can be treated as external storage. It consists of a sequence of bytes residing on the disk. Groups of related data can be stored in a single file. A program can create, read, and write to a file. Unlike an array, the data in the file is retained even after the program finishes its execution. It's a permanent storage medium.

Declaring a File Pointer

In C language, in order to declare a file, we use a file pointer. A file pointer is a pointer variable that specifies the next byte to be read or written to. Every time a file is opened, the file pointer points to the beginning of the file. A file is declared as follows:

FILE *fp;
//fp is the name of the file pointer

Opening, Creating, Closing

C language provides a number of functions to perform file handling. fopen() is used to create a new file or open an existing file. The syntax is as follows:

fp = FILE *fopen(const char *filename, const char *mode);

fp is the file pointer that holds the reference to the file, the filename is the name of the file to be opened or created, and mode specifies the purpose of opening a file such as for reading or writing. FILE is an object type used for storing information about the file stream.

A file can be opened in different modes. Below are some of the most commonly used modes for opening or creating a file.

  • r : opens a text file in reading mode.
  • w : opens or creates a text file in writing mode.
  • a : opens a text file in append mode.
  • r+ : opens a text file in both reading and writing mode. The file must exist.
  • w+ : opens a text file in both reading and writing mode. If the file exists, it's truncated first before overwriting. Any old data will be lost. If the file doesn't exist, a new file will be created.
  • a+ : opens a text file in both reading and appending mode. New data is appended at the end of the file and does not overwrite the existing content.
  • rb : opens a binary file in reading mode.
  • wb : opens or creates a binary file in writing mode.
  • ab : opens a binary file in append mode.
  • rb+ : opens a binary file in both reading and writing mode, and the original content is overwritten if the file exists.
  • wb+: opens a binary file in both reading and writing mode and works similar to the w+ mode for binary files. The file content is deleted first and then new content is added.
  • ab+: opens a binary file in both reading and appending mode and appends data at the end of the file without overwriting the existing content.

A file needs to be closed after a read or write operation to release the memory allocated by the program. In C, a file is closed using the fclose() function. This returns 0 on success and EOF in the case of a failure. An EOF is defined in the library called stdio.h. EOF is a constant defined as a negative integer value which denotes that the end of the file has reached.

fclose( FILE *fp );

Writing and Reading

Now that you've learned how to create a file in various modes and also how to close a file, you must be wondering: how would you perform input and output operations in a file? The fputc() and fputs() functions are used to write characters and strings respectively in a file. Let's see how it's used.

int fputc( int c, FILE *fp );

This function writes a character c into the file with the help of the file pointer fp. It returns EOF in the case of an error.

int fputs( const char *s, FILE *fp );

This function writes string s to the file with the help of the reference pointer fp.

The fgetc(), fgets(), and fscanf() are functions used in C programming language to read characters or strings from a file. Let's discuss these, one by one.

int fgetc( FILE *fp );

The fgetc() accepts the file pointer as the parameter and reads one character at a time from the file. The return value is the character that's read. In case of any error, it returns EOF.

char *fgets( char *buffer, int n, FILE *fp );

fgets() reads up to n-1 characters from the input stream referenced by fp. The string read is copied into the character buffer called buffer. It appends the characters read to the string, but terminates when a null character is encountered.

int fscanf(FILE *fp, const char *format, char *buffer)

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account