File Handling in C

Google Advertisements

Prev Tutorial Next Tutorial

File Handling in C

File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our data in Secondary memory (Hard disk). All files related function are available in stdio.h header file.

How to achieve File Handling in C

For achieving file handling in C we need follow following steps

  • Naming a file
  • Opening a file
  • Reading data from file
  • Writing data into file
  • Closing a file

Functions use in File Handling in C

S.NoFunctionOperation
1fopen()To create a file
2fclose()To close an existing file
3getc()Read a character from a file
4putc()Write a character in file
5fprintf()To write set of data in file
6fscanf()To read set of data from file.
5getw()To read an integer from a file
6putw()To write an integer in file

Defining and Opening a File

Data structure of file is defined as FILE in the standard I/O function. So all files should be declared as type FILE.

Before opening any file we need to specify for which purpose we open file, for example file open for write or read purpose.

Syntax

FILE *fp;
pf=fopen("filename", "mode");

Here fp declare a variable as a pointer to the data type FILE.

Closing a File

A file must be close after completion of all operation related to file. For closing file we need fclose() function.

Syntax

fclose(Filepointer);

File Opening mode

S.NoModeMeaningPurpose
1rReadingOpen the file for reading only.
2wWritingOpen the file for writing only.
3aAppendingOpen the file for appending (or adding) data to it.
4r+Reading + WritingNew data is written at the beginning override existing data.
5w+Writing + ReadingOverride existing data.
6a+Reading + AppendingTo new data is appended at the end of file.

Input/Output Operation on files

To perform Input/Output Operation on files we need below functions.

S.NoFunctionOperationSyntax
1getc()Read a character from a filegetc( fp)
2putc()Write a character in fileputc(c, fp)
3fprintf()To write set of data in filefprintf(fp, "control string", list)
4fscanf()To read set of data from file.fscanf(fp, "control string", list)
5getw()To read an integer from a file.getw(fp)
6putw()To write an integer in file.putw(integer, fp)

Write data in File

Example

#include<stdio.h>
#include<conio.h>

void main()
{
FILE *fp;
char ch[20];
clrscr();
fp=fopen("hh.txt", "w");
printf("Enter any Text: ");
scanf("%s",&ch); // Read data from keyboard
fprintf(fp,"%s",ch); // Write data in file
fclose(fp);
getch();
}

Prev Tutorial Next Tutorial

Google Advertisements

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs