File Input output in C

File input output in c

Preview
You must first complete Steps to create a data file in C before viewing this Lesson



File Input output in C

Different predefined functions of C language for file input/output are:

1.  fputc()/putc() function

These functions are used to insert single character to a specified file.

Syntax:

fputc(C, Fpointer);

OR

putc(C, Fpointer);

 

C refers to character variable or constant whose value should be stored into a file.

Fpointer  is the file pointer variable .

 Program to demonstrate fputc()/putc().
#include<stdio.h>
int main()
{
FILE *f1;
char s;
f1=fopen(“myfile.txt”,”w”);
printf(“Enter any character:”);
scanf(“%c”,&s);
fputc(s,f1);  //or putc(s,f1);
fclose(f1);
printf(“character stored in file”);
return(0);
}
Output
Enter any character :k
character stored in file




2.  fgetc()/getc() function

This function is used to read single character from a file.

Syntax:

v=fgetc( Fpointer);

OR

v=getc( Fpointer);

v is character variable to hold character read from file.

Fpointer is the file pointer variable . 

 Program to demonstrate fgetc()/getc()
#include<stdio.h>
int main()
{
FILE *f1;
char s;
f1=fopen(“myfile.txt”,”r”);
printf(“File contains:”);
s=fgetc(f1);  //or s=getc(f1);
printf(“%c”,s);
fclose(f1);
return(0);

}
Output
File contains:k

3.  putw() function

putw()  is used to insert an integer value to a file.

Syntax :

putc(Val, Fpointer);

Val refers to integer variable or constant whose value is to be written into a file.

Fpointer refers to the file pointer variable. 

Program to demonstrate putw().
#include<stdio.h>
int main()
{
FILE *f1;
int n;
f1=fopen(“integer.txt”,”w”);
printf(“Enter any integer value:”);
scanf(“%d”,&n);
putw(n,f1);
fclose(f1);
printf(“Integer value inserted” );
return(0);
}
Output
Enter any integer value:100
Integer value inserted

4.  getw() function

This function is used to read integer value from a file.

Syntax

v=getw( F);

v refers to integer variable to store integer value read from a file.

Fpointer is the file pointer variable. 

 Program to demonstrate getw().
#include<stdio.h>
int main()
{
FILE *f1;
int n;
f1=fopen(“integers.txt”,”r”);
printf(“File contains”);
n=getw(f1);
printf(“%d”,n);
fclose(f1);
return(0);
}
Output
File contains 100

5. fputs() function

fputs()  function is used to write a string value to a file.

Syntax:

fputs(S , Fpointer);

S represents the string variable or constant whose value is to be written into a file.

Fpointer represents file pointer variable. 

Program to demonstrate fputs().
#include<stdio.h>
int main()
{
FILE *f1;
char n[100];
f1=fopen(“words.txt”,”w”);
printf(“Enter a string=”);
gets(n);
fputs(n,f1); //inserstring value in file
fclose(f1);
printf(“\nstring stored in file”);
return(0);
}
Output
Enter a string=This is a string value
string stored in file

6. fgets() function

fgets()  function is used to get a string from a specified fileS

Syntax:

fgets(S , N, Fpointer);

S represents the string variable to store string value read from file.

N represents the number of characters to be extracted from the file.

F refers to the file pointer variable to refer a file.

 Program to demonstrate fgets().
#include<stdio.h>
int main()
{
FILE *f1;
char str[100];
f1=fopen(“words.txt”,”r”);
fgets(str,100,f1); 
printf(“\nFile contains:”);
printf(“%s”,str);
fclose(f1);
return(0);
}
Output
File contains: This is a string value




7. fprintf() function

This function is used to insert values of different types to a file.

Syntax:

fprintf(Fpointer,”Control-String”, var1,var2,….);

F is the file pointer variable used to refer a file.

Control-string contains conversion characters like %d, %f, %s to insert values of different data types into a file.

var1,var2,….  are variables or values we want to insert into file.

 Program to demonstrate fprintf() function
#include<stdio.h>
int main()
{
FILE *f1;
int empid;
char ename[20];
f1=fopen(“emp.dat”,”w”);
printf(“Enter employee id:”);
scanf(“%d”,&empid);
printf(“Enter employee name:”);
fflush(stdin);
gets(ename);
fprintf(f1,”%d%s”,empid,ename);
fclose(f1);
return(0);
} 
Output
Enter employee id:101
Enter employee name:Sahil

8. fscanf() function

This function is used to read different types of data from a file.

Syntax:

fscanf(Fpointer,”Control-String”,var1,var2,….);

Fpointer represents file pointer variable to refer to a file.

Control-string can contain conversion characters like %d, %f, %s to read values of different types from file.

var1,var2,….  are variables to contain values read from the file. 

Program to demonstrate fscanf() function.
#include<stdio.h>
int main()
{
FILE *f1;
int empid;
char ename[20];
f1=fopen(“emp.dat”,”r”);

fscanf(f1,”%d%s”,&empid,ename);
printf(“\nEmployee id=%d”,empid);
printf(“\nEmployee Name=%s”,ename);
fclose(f1);
return(0);
}
Output
Rollno of student=1
Name of student=Amit 



Best Books of C

Lesson tags: fgetc() function in c, fgets() function in c, File Input output in C in c, fprintf() function in c, fputc() function in c, fputs() function in c, fread() function in c, fscanf() function in c, fwrite() function in c, getc() function in c, getw() function in c, put content into file in c, putc()  in c, putw() function in c, read contents of file in c
Back to: C Programming Language