A file has to be opened before the beginning of reading and writing operations. Opening a file creates a link between the operating system and the file function.
Syntax for opening a file:
FILE *fp; fp = fopen( " filename with extension ", " mode " );
Opening of file in detail:
FILE: structure defined in stdio.h header file. FILE structure provides us the necessary information about a FILE.
fp: file pointer which contains the address of the structure FILE.
fopen(): this function will open file with name “filename” in specified “mode”.
Different reading modes:
- for binary files: rb, rb+, r+b
|r mode||r+ mode|
|Purpose||Opens an existing text file for reading purpose.||Opens a text file for both reading and writing.|
|fopen Returns if FILE doesn’t exists||NULL||Create New File|
|fopen returns if FILE exist||Returns a pointer to the FILE object.||New data is written at the start of existing data|
|file pointer position||at the first char of the file||at the first char of the file|
C program for opening file in r mode:
Note: File opened should be closed in the program after processing.
C program for opening file in r+ mode:
geeksforgeeks online reference
- C program to check whether the file is JPEG file or not
- C++ program to create a file
- C | File Handling | Question 1
- C | File Handling | Question 2
- C | File Handling | Question 3
- C | File Handling | Question 4
- C | File Handling | Question 5
- Basics of File Handling in C#
- C program for file Transfer using UDP
- How to write your own header file in C?
- Basics of File Handling in C
- time.h header file in C with Examples
- <complex.h> header file in C with Examples
- C Program to find size of a File
- Lex program to search a word in a file
- C program to append content of one text file to another
- C program to reverse the content of the file and print it
- C Program for Lower Case to Uppercase and vice-versa in a file
- Sorting integer data from file and calculate execution time
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rishiSingh2