C language file operations

In this chapter we shall study about below topics:
1. Introduction
2. Common file functions
3. Basic file operation functions
4. File open modes
5. Binary Modes
6. File Input and Output operations

1. Introduction

A file is a sequence of bytes that are stored in the secondary memory. There are various kinds of files, like text file, program file, executable file, data file and so on.
The data can be modified, deleted from a file with the help of streams. A stream is nothing but a flow of data in a sequence. The point where the input is received is called as source stream or input stream and the data where it is printed is called as output stream.

2. Common file functions

fopen()				Creates new file to read or write
fclose()			Closes the file
closeall()			closes all files opened with fopen
getc()				Reads input one by one
putc()				writes character one by one
fseek()				Sets the pointer position anywhere in the file.
feof()				Detects the end of file
ftell()				Returns the current pointer position
rename()			Renames the file name.

3. Basic file operation functions

1. To open any file, “FILE” pointer variable should be created. Then we request OS to give permission to access the file.
FILE *fp;
2. To open a file “fopen()” is called, if the file exist then a pointer to that file will be returned, else NULL will be returned.
fp = fopen(“myfile.txt”, “r”);
3. To read a opened file, we use fgetc() function.
ch = fgetc(fp);
4. To close a file we use fclose() function.
fclose(fp).
It is important to close all the files opened. Else there are chances of file getting corrupted.

4. File open modes

1. Write Mode:

In this mode a new file will be created in write mode, if a file already exists then it’s content will be over written without any confirmation.
“w” is used to open in write mode.
fp = fopen(“myFile.txt”, “w”);

2. Read Mode:

In this mode a file will be opened in read mode, if a file already exists then it will return a pointer to the first character in that file. Else it will return NULL.
“r” is used to open in read mode.
fp = fopen(“myFile.txt”, “r”);

3. Append Mode:v

In this mode a new file will be created if the file doesn’t exist. If a file already exists then a pointer pointing to the last character is returned for appending the data.
“a” is used to open in write mode.
fp = fopen(“myFile.txt”, “a”);

4. Read write +

Used to read and write the file. If the file exists then its contents are destroyed.
“w+” is used to open in write mode.
fp = fopen(“myFile.txt”, “w+”);

5. Read append +

Used to read and append the file. If the file exists then its contents are destroyed.
a+” is used to open in write mode.
fp = fopen(“myFile.txt”, “a+”);

5. Binary Modes

1. wb
write a file in binary mode
fp = fopen(“myFile.dat”, “wb”);
2. rb
Read a file in binary mode.
fp = fopen(“myFile.dat”, “rb”);

6. File Input and Output operations

1. fprintf()

This function is used to write strings, integers and floating into a file.

Example:

#include<stdio.h>

int main()
{
	FILE *fp = NULL;

	char text[40]= {'\0'};

	// open a new file "hello.txt"
	fp = fopen("hello.txt", "w");

	printf("Enter some text\n");
	gets(text);

	//write the entered text to the file pointed by "fp"
	fprintf(fp, "%s", text );

	//close the file pointer
	fclose(fp);
}

Output:

Enter some text
www.prodevelopertutorial.com
If you see the directory where you have compiled the program, you will find a file with the name “hello.txt”. “www.prodevelopertutorial.com” will be written inside it.

2. fscanf()

This function reads a character from a file specified by file pointer.

Example:

#include<stdio.h>

int main()
{
	FILE *fp = NULL;

	int inum = 120;
	float fnum = 12.34;
	
	int inum_read;
	float fnum_read;
	
	// open a new file "hello.txt"
	fp = fopen("hello.txt", "w");

	// enter the values of inum and fnum into the file
	fprintf(fp, "%d %f", inum, fnum );

	// close the file pointer, so that the data will be saved.
	fclose(fp);
	fp = NULL;

	// open the same file in read mode
	fp = fopen("hello.txt", "r");

	// copy the contents from the file and assign them to the variables
	fscanf(fp, "%d %f", &inum_read, &fnum_read);

	printf(" int value = %d float value = %f \n", inum_read, fnum_read );

	//close the file pointer
	fclose(fp);
}

Output:

int value = 120 float value = 12.340000

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *