File Handling in C

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 Advertisment

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

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

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements