0% found this document useful (0 votes)
5 views

File Handling

The document provides an overview of file handling in the C programming language, detailing operations such as creating, opening, reading, writing, and closing files. It explains the usage of the fopen() function with various modes for file access, as well as functions for reading (fscanf(), fgets(), fgetc()) and writing (fprintf(), fputs(), fputc()) data to files. Additionally, it includes example code for creating a file and copying contents from one file to another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

File Handling

The document provides an overview of file handling in the C programming language, detailing operations such as creating, opening, reading, writing, and closing files. It explains the usage of the fopen() function with various modes for file access, as well as functions for reading (fscanf(), fgets(), fgetc()) and writing (fprintf(), fputs(), fputc()) data to files. Additionally, it includes example code for creating a file and copying contents from one file to another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

File Handling is the storing of data in a file using a program.

In C
programming language, the programs store results, and other data of the
program to a file using file handling in C. Also, we can extract/fetch data
from a file to work with it in the program.
The operations that you can perform on a File in C are −
● Creating a new file
● Opening an existing file
● Reading data from an existing file
● Writing data to a file
● Moving data to a specific location on the file
● Closing the file
Creating or opening file using fopen()
The fopen() function is used to create a new file or open an existing file
in C. The fopen function is defined in the stdio.h header file.
Now, lets see the syntax for creation of a new file or opening a file
file = fopen(“file_name”, “mode”)
This is a common syntax for both opening and creating a file in C.
Mode = “r” − open for reading, this mode will open the file for reading purpose only, i.e. the contents
can only be viewed, nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
Mode = “rb” − open for reading in binary mode, this mode will open the file for reading in binary
mode only, i.e. the contents can only be viewed and nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
Mode = “w” − open for writing only, this mode will open the file if present in the current directory for
writing only i.e. reading operation cannot be performed. If the file is not present in the current
directory, the program will create a new file and open it for writing.
If we open a file that contains some text in it, the contents will be overwritten.
Mode = “wb” − open for writing in binary mode, this mode will open the file if present in the current
directory for writing in binary mode i.e. reading operation cannot be performed. If the file is not present
in the current directory, the program will create a new file and open it for writing in binary mode.
If we open a file that contains some text in it, the contents will be overwritten.

Mode = “a” − open for append only, this mode will open the file if present in the current directory
for writing only i.e. reading operation cannot be performed. If the file is not present in the current
directory, the program will create a new file and open it for writing. If we open a file that contains some
text in it, the contents will not be overwritten; instead the new text will be added after the existing text
in the file.
Mode = “ab” − open for append in binary, this mode will open the file if present in the current
directory for writing in binary only i.e. reading operation cannot be performed. If the file is not present
in the current directory, the program will create a new file and open it for writing in binary.
If we open a file that contains some text in it, the contents will not be overwritten; instead the new text
will be added after the existing text in the file.
Mode = “r+” − open for reading and writing both, this mode will open the file for both reading and
writing purposes i.e. both read and write operations can be performed to the file.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
If we open a file that contains some text in it and write something, the contents will be overwritten.
Mode = “rb+” − open for reading in binary mode, this mode will open the file for reading in binary
mode only, i.e. the contents can only be viewed and nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
If we open a file that contains some text in it and write something, the contents will be overwritten.
Mode = “w” − open for writing and reading, this mode will open the file if present in the current
directory for writing and reading operation both. If the file is not present in the current directory, the
program will create a new file and open it for reading and writing.
If we open a file that contains some text in it, the contents will be overwritten.

Reading Data for from an existing file


We can read content of a file in c using the fscanf() and fgets() and fgetc() functions. All are used to
read contents of a file. Let’s see the working of each of the function −

fscanf()
The fscanf() function is used to read character set i.e strings from the file. It returns the EOF, when all
the content of the file are read by it.

Syntax
int fscanf(FILE *stream, const char *charPointer[])

fgets()
The fget() function in C is used to read string from the stream.

Syntax
char* fgets(char *string, int length, FILE *stream)

fgetc()
The fgetc() function in C is used to return a single character from the file. It gets a character from the
file and returns EOF at the end of file.

Syntax
char* fgetc(FILE *stream)

Writing Data to a file in C


We can write data to a file in C using the fprintf(), fputs(), fputc() functions. All are used to write
contents to a file.
Let’s see the working of each of the function −
fprintf()
The fprintf() function is used to write data to a file. It writes a set of characters in a file.

Syntax
int fprintf(FILE *stream, char *string[])

fputf()
The fputf() function in C can be used to write to a file. It is used to write a line (character line) to the
file.

Syntax
int fputs(const char *string, FILE *stream)

fputc()
The fputc() function is used to write a single character to the file.

Syntax
int fputc(char character , FILE *stream)

fclose()
The fclose() function is used to close the open file. We should close the file after performing
operations on it to save the operations that we have applied to it.

Syntax
fclose(FILE *stream)

45. Create and store information in a file


#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
int salary;
/* open for writing */
fptr = fopen("emp.txt", "w");
if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age:\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary:\n");
scanf("%d", &salary);
fprintf(fptr, "Salary = %d\n", salary);
fclose(fptr);
}

46. Copying the contents from one file to another file


#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt

You might also like