Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Opening Files with fopen | Introduction and File Streams
C File Handling Basics

bookOpening Files with fopen

The fopen function is used in C to open files for reading, writing, or appending. It is part of the standard library and allows you to create a connection between your program and a file on disk. When you call fopen, you specify both the name of the file and the mode in which you want to open it, such as reading or writing. The function returns a pointer of type FILE *, which you then use with other file handling functions to perform operations on the file.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> int main() { FILE *file; file = fopen("data.txt", "r"); if (file == NULL) { fprintf(stderr, "Failed to open file.\n"); return 1; } printf("File opened successfully.\n"); return 0; }
"r"
expand arrow

Open a file for reading. The file must exist;

"w"
expand arrow

Open a file for writing. If the file exists, its contents are erased. If it does not exist, a new file is created;

"a"
expand arrow

Open a file for appending. Data is added to the end of the file if it exists, or a new file is created;

"rb"
expand arrow

Open a file for reading in binary mode. The file must exist;

"wb"
expand arrow

Open a file for writing in binary mode. If the file exists, its contents are erased, or a new file is created;

"ab"
expand arrow

Open a file for appending in binary mode. Data is added to the end of the file, or a new file is created.

Note
Check fopen Return Value

Always check if fopen returns NULL before performing any file operations. If fopen fails to open the file, it returns NULL, which can prevent your program from crashing or corrupting data.

question mark

Which mode should you use to open a file for appending data?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 12.5

bookOpening Files with fopen

Swipe to show menu

The fopen function is used in C to open files for reading, writing, or appending. It is part of the standard library and allows you to create a connection between your program and a file on disk. When you call fopen, you specify both the name of the file and the mode in which you want to open it, such as reading or writing. The function returns a pointer of type FILE *, which you then use with other file handling functions to perform operations on the file.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> int main() { FILE *file; file = fopen("data.txt", "r"); if (file == NULL) { fprintf(stderr, "Failed to open file.\n"); return 1; } printf("File opened successfully.\n"); return 0; }
"r"
expand arrow

Open a file for reading. The file must exist;

"w"
expand arrow

Open a file for writing. If the file exists, its contents are erased. If it does not exist, a new file is created;

"a"
expand arrow

Open a file for appending. Data is added to the end of the file if it exists, or a new file is created;

"rb"
expand arrow

Open a file for reading in binary mode. The file must exist;

"wb"
expand arrow

Open a file for writing in binary mode. If the file exists, its contents are erased, or a new file is created;

"ab"
expand arrow

Open a file for appending in binary mode. Data is added to the end of the file, or a new file is created.

Note
Check fopen Return Value

Always check if fopen returns NULL before performing any file operations. If fopen fails to open the file, it returns NULL, which can prevent your program from crashing or corrupting data.

question mark

Which mode should you use to open a file for appending data?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt