Tutorial references

c
c++
c#

Tutorial references

chapters

File Handling in C

File handling

File Handling in c language is used to open, read, write, search or close file.

File Handling concept used for store data permanently in computer memory.

NOTE:- First include "stdlib.h" header file in program.

File Opening

Syntax:

FILE *fopen( const char * filename, const char * mode );

fopen() is used to open a file in read,write mode.

Mode

Mode Description
r It opens existing text file in read mode.
w It opens existing text file in write mode. If file is not exists then it creates file and open in write mode.
a It opens existing text file in append mode.
r+ It opens existing text file in read and write mode.
w+ It opens existing text file in read and write mode. First it does empty existing file then writes data in file.
a+ It opens existing text file in read mode and write mode for append.
rb It opens binary file in read mode.
wb It opens binary file in write mode.
ab It opens binary file in append mode.
rb+ It opens binary file in read and write mode.
wb+ It opens binary file in read and write mode.
ab+ It opens binary file in read mode and write mode for append.

fclose()

It is a predefine function. It is used to close file that is opened by fopen().

Syntax:

int fclose( FILE *fp );

fputc()

It is a predefine function. It is used to write single character in file.

Syntax:

int fputc(int ch, FILE *stream);

Example

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

void main(){
FILE *fl;
char ch;
clrscr();
fl = fopen("sabbla.txt","w");
printf("\n Enter character: ");
scanf("%c",&ch);
fputc(ch,fl);
fclose(fl);
getch();
}

Run program

fgetc()

It is a predefine function. It is used to read single character from file.

Syntax:

int fgetc(FILE *stream);

Example

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

void main(){
FILE *fl;
char ch
clrscr();
fl = fopen("sabbla.txt","r");
ch = fgetc(fl);
printf("\n File character is: %c",ch);
fclose(fl);
getch();
}

Run program

Example

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

void main(){
FILE *fl;
char ch;
clrscr();
fl = fopen("sabbla.txt","r");
while((ch=fgetc(fl))!=EOF){
printf("\n File character is: %c",ch);
}
fclose(fl);
getch();
}

Run program

fputs()

It is a predefine function. It is used to write single line of characters in file.

Syntax:

int fputs(const char *s, FILE *stream);

Example

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

void main(){
FILE *fl;
char str[200];
clrscr();
fl = fopen("sabbla.txt","w");
printf("\n Enter your good name: ");
gets(str);
fputs(str,fl);
fclose(fl);
getch();
}

Run program

fgets()

It is a predefine function. It is used to read characters from file according to given length.

Syntax:

char* fgets(char *s, int length, FILE *stream);

Example

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

void main(){
FILE *fl;
char str[200];
clrscr();
fl = fopen("sabbla.txt","r");
fgets(str,100,fl);
printf("your data: %s",str);
fclose(fl);
getch();
}

Run program

fprintf()

It is a predefine function. It is used to write set of characters in file.

Syntax:

int fprintf(FILE *stream, const char *format [, argument, ...]);

Example

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

void main(){
FILE *fl;
char str[200];
clrscr();
fl = fopen("sabbla.txt","w");
printf("\nEnter your data: ");
gets(str);
fprintf(fl,str);
fclose(fl);
getch();
}

Run program

fscanf()

It is a predefine function. It is used to read set of characters from file.

Syntax:

int fscanf(FILE *stream, const char *format [, argument, ...]);

Example

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

void main(){
FILE *fl;
char str[200];
clrscr();
fl = fopen("sabbla.txt","r");
while(fscanf(fl, "%s", str)!=EOF){
printf("%s ",str);
}
fclose(fl);
getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018