
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Operations on Files in C Language
A file is a collection of data stored in secondary memory. The data is entered directly from the keyboard, files are used to store both information and programs. The following operations can be performed on files in the C language ?
File Naming and Opening
The syntax for opening and naming file is as follows ?
FILE *File pointer;
For example, FILE * fptr; opens a file in a specified mode and returns a file pointer.
File pointer = fopen ("File name", "mode");
For example, fptr = fopen ("sample.txt", "r") opens a file named "sample.txt" in read mode and assigns the file pointer to fp.
FILE *fp; fp = fopen ("sample.txt", "w");
File Opening Modes
The modes of opening the file in C language are explained below ?
Modes | Description |
---|---|
r | File is opened for reading |
w | File is opened for writing |
a+ | File opened for append |
r+ | File opened for reading & writing |
w+ | File opened for writing & reading |
a+ | File opened for append & reading |
rt | Text file is opened for reading |
wt | Text file is opened for writing |
at | Text file is opened for appending |
r+t | Text file is opened for reading & writing |
w+t | Text file is opened for both writing & reading |
a+t | Text file is opened for both appending & reading |
rb | Binary file is opened for reading |
wb | Binary file is opened for writing |
ab | Binary file is opened for appending |
r+b | Binary file is opened for both reading & writing |
w+b | Binary file is opened for both writing & reading |
a+b | Binary file is opened for both appending & reading. |
Opening a File in Writing Mode
Writes a specified number of objects from an array to an output stream, this produces the file position indicator.
The syntax is as follows ?
FILE *fp; fp =fopen ("sample.txt", "w");
If the file does not exist, a new file is created. If the file exists, the old content is erased and the new content is stored.
Opening a File in a Read Mode
Reads a specified number of objects into an array from an input stream, that determines the file position indicator.
The syntax is as follows ?
FILE *fp fp =fopen ("sample.txt", "r");
If the file does not exist, the fopen function returns a NULL value. If the file exists, data is read successfully from the file.
Opening a File in Append Mode
Append mode is used to add content to the existing file. When we open a file in Append mode, then the cursor is placed at the end of the current data in the file.
The syntax is as follows ?
FILE *fp; fp =fopen ("sample.txt", "a");
If the file does not exist, a new file will be created. If the file exists, the new content will be added to the old content.
Mode | Exit | Not Exit |
---|---|---|
R | Read | fp="NULL" |
W | Current Content | New file will be created |
A | Old ContentCurrent Content |
New file will be Created |
Closing the File
Closes the given file and redirects the data to the OS and deletes the unread data. Using the pointer after fclose returns an undefined behavior.
The syntax is as follows ?
int fclose(FILE *stream);
Example
Here is a C program for file operations. This program copies the content of one file to another. It opens the source file for reading and writing the files.
#include <stdio.h> #include <stdlib.h> int main(){ FILE *fptr1, *fptr2; char filename[100], c; printf("Enter the filename to open for reading
"); scanf("%s",filename); // Open one file for reading fptr1 = fopen(filename, "r"); if (fptr1 == NULL){ printf("Cannot open file %s
", filename); exit(0); } printf("Enter the filename to open for writing
"); scanf("%s", filename); // Open another file for writing fptr2 = fopen(filename, "w"); if (fptr2 == NULL){ printf("Cannot open file %s
", filename); exit(0); } // Read contents from file c = fgetc(fptr1); while (c != EOF){ fputc(c, fptr2); c = fgetc(fptr1); } printf("
Contents copied to %s", filename); fclose(fptr1); fclose(fptr2); return 0; }
Output
When the above program is executed, it produces the following result ?
Enter the filename to open for reading file2.txt Enter the filename to open for writing file1.txt Contents copied to file1.txt