File Handling in CFi

Posted on

File Dealing with


File Dealing with in C

 

File Dealing with idea in C language is used for retailer a knowledge completely in laptop. Utilizing this idea we are able to retailer our information in Secondary reminiscence (Exhausting disk). All recordsdata associated perform can be found in stdio.h header file.

Learn how to obtain File Dealing with in C

For reaching file dealing with in C we’d like comply with following steps

  • Naming a file
  • Opening a file
  • Studying information from file
  • Writing information into file
  • Closing a file

Capabilities use in File Dealing with in C

S.No Perform Operation
1 fopen() To create a file
2 fclose() To shut an present file
3 getc() Learn a personality from a file
4 putc() Write a personality in file
5 fprintf() To write down set of information in file
6 fscanf() To learn set of information from file.
5 getw() To learn an integer from a file
6 putw() To write down an integer in file

Defining and Opening a File

Information construction of file is outlined as FILE in the usual I/O perform. So all recordsdata needs to be declared as kind FILE.

Earlier than opening any file we have to specify for which goal we open file, for instance file open for write or learn goal.

Syntax

Right here fp declare a variable as a pointer to the info kind FILE.

Closing a File

A file have to be shut after completion of all operation associated to file. For closing file we’d like fclose() perform.

Syntax

File Opening mode

S.No Mode That means Function
1 r Studying Open the file for studying solely.
2 w Writing Open the file for writing solely.
3 a Appending Open the file for appending (or including) information to it.
4 r+ Studying + Writing New information is written at first override present information.
5 w+ Writing + Studying Override present information.
6 a+ Studying + Appending To new information is appended on the finish of file.

Enter/Output Operation on recordsdata

To carry out Enter/Output Operation on recordsdata we’d like under capabilities.

S.No Perform Operation Syntax
1 getc() Learn a personality from a file getc( fp)
2 putc() Write a personality in file putc(c, fp)
3 fprintf() To write down set of information in file fprintf(fp, “management string”, checklist)
4 fscanf() To learn set of information from file. fscanf(fp, “management string”, checklist)
5 getw() To learn an integer from a file. getw(fp)
6 putw() To write down an integer in file. putw(integer, fp)

Write information in File

Instance