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

There are many functions provided by C language to read or write data into a file. These include the following functions:

1.  fputc() function

fputc() function is used to write a single character to a specified file at the current file position and then increment the file position pointer.

The fputc() function returns an integer representing  the character written. If a write error occurs the error indicator is set and fputc returns EOF.

The syntax of fputc() function is:

fputc(Character, Filepointer);

Character represents the character value to be written into a file.

Filepointer refers to the file pointer variable which is used to refer to a file.

 Program to insert a character in a text file using fputc().
#include<stdio.h>
int main()
{
FILE *f;
char ch;
f=fopen(“data.txt”,”w”);
printf(“Enter any character you want to insert into file:”);
ch=getchar();
fputc(ch,f);
fclose(f);
printf(“File created”);
return(0);
}
Output
Enter any character you want to insert into file:H
File created

Note: After running this program, a new file with name data.txt will be created which contains the character entered by the user at run time.



2.  fgetc() function

fgetc() function is used to read a single character from a file. This function returns an int which means that when it reads a normal character from the file.

It converts the character into int. On the other hand, when the pointer reaches end of the file, fgetc() will return EOF. The syntax of fgetc() function is:

char var=fgetc( Filepointer);

var represents the character variable which will contain the character read from a file.

Filepointer refers to the file pointer variable which is used to refer to a file from where we want to read the character. 

 Program to read a character from a text file using fgetc()
#include<stdio.h>
int main()
{
FILE *f;
char ch;
f=fopen(“data.txt”,”r”);
printf(“Contents of file are:”);
ch=fgetc(f);
printf(“%c”,ch);
fclose(f);
return(0);

}
Output
Contents of file are:H

3 . putc()  

putc()  is a macro used to write a single character to a specified file at the current file position. The putc() function returns an integer representing  the character written. If a write error occurs putc() returns EOF.
The syntax of putc() function is:
putc(Character, Filepointer);
Character represents the character value to be written into a file.
Filepointer refers to the file pointer variable which is used to refer to a file.

Program to insert a character in a text file using putc()
#include<stdio.h>
int main()
{
FILE *f;
char ch;
f=fopen(“data.txt”,”w”);
printf(“Enter any character you want to insert into file:”);
ch=getchar();
putc(ch,f);
fclose(f);
printf(“File created”);
return(0);
}
Output
Enter any character you want to insert into file:H
File created

Note: After running this program, a new file with name data.txt will be created which contains the character entered by the user at run time.*/



4. getc() function

getc() function is used to read a single character from a file. This function returns an int which means that when it reads a normal character from the file.

It converts the character into int. On the other hand, when the pointer reaches end of the file, getc() will return EOF. The syntax of getc() function is:

char var=getc( Filepointer);

var represents the character variable which will contain the character read from a file.

Filepointer refers to the file pointer variable which is used to refer to a file from where we want to read the character. 

Program to read a character from a text file using getc()
#include<stdio.h>
int main()
{
FILE *f;
char ch;
f=fopen(“data.txt”,”r”);
printf(“Contents of file are:”);
ch=getc(f);
printf(“%c”,ch);
fclose(f);
return(0);
}
Output
Contents of file are:H

5.  putw() function

putw()  is a predefined function of C language which is used to write an integer value to a specified file at the current file position. The putw() function returns an integer representing  the value written to the file. If a write error occurs putw() returns EOF.

The syntax of putw() function is:

putc(Value, Filepointer);

Value represents the integer value to be written into a file.

Filepointer refers to the file pointer variable which is used to refer to a file. 

Program to insert an integer value into a text file using putw().
#include<stdio.h>
int main()
{
FILE *f;
int ch;
f=fopen(“numbers.txt”,”w”);
printf(“Enter any integer number you want to insert into file:”);
scanf(“%d”,&ch);
putw(ch,f);
fclose(f);
printf(“File created…” );
return(0);
}
Output
Enter any integer number you want to insert into file:900
File created…

Note: After running this program, a new file with name numbers.txt will be created which contains the integer value entered by the user at run time.*/


6.  getw() function

getw() function is used to read an integer value from a file. This function returns an int which means that when it reads a normal value from the file.

It converts the value into int. On the other hand, when the pointer reaches end of the file, getw() will return EOF. The syntax of getw() function is:

int var=getw( Filepointer);

var represents the integer variable which will contain the value read from a file.

Filepointer refers to the file pointer variable which is used to refer to a file from where we want to read the integer value. 

 Program to read an integer value from a text file using getw().
#include<stdio.h>
int main()
{
FILE *f;
int ch;
f=fopen(“numbers.txt”,”r”);
printf(“Contents of file are:”);
ch=getw(f);
printf(“%d”,ch);
fclose(f);
return(0);
}
Output
Contents of file are:900

7. fputs() function

fputs()  is a predefined function of C language which is used to write a string value to a specified file at the current file position. fputs() function doesn’t insert newline character at the end of string.

It doesn’t insert null character from the string into file. The syntax of fputs() function is:

fputs(Str , Filepointer);

Str represents the string value to be written into a file.

Filepointer refers to the file pointer variable which is used to refer to a file. 

Program to insert a string value into a text file using fputs()
#include<stdio.h>
void main()
{
FILE *f;
char sentence[50];
f=fopen(“data.txt”,”w”);
printf(“\nEnter any sentence=”);
gets(sentence);
fputs(sentence,f); //To insert a string value into a file
fclose(f);
printf(“\nFile created”);
return(0);
}
Output
Enter any sentence=i love india
File created

Note: After running this program, a new file with name data.txt will be created which contains the string value entered by the user at run time.*/

8. fgets() function

fgets()  is a predefined function of C language which is used to extract a string from a specified file. ray.
The syntax of fputs() function is:

fputs(Str , N, Filepointer);

Str represents the string value to be written into a file.

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

Filepointer refers to the file pointer variable which is used to refer to a file.

The fgets() function reads characters from the current file pointer position onwards until either a newline character or an EOF is received or until the specified limit is reached. After the characters have been read, a null is stored in the array immediately after the last character is read. A newline character will be retained and will be a part of the string variable  pointed to by Str.

Second argument N is used to prevent fgets() from reading a too long string and overflowing the string variable. 

 Program to read a string from a text file using fgets()
#include<stdio.h>
int main()
{
FILE *f;
char sentence[50];
f=fopen(“data.txt”,”r”);
fgets(sentence,50,f);  //To read a string value from a file
printf(“\nContents of file are=”);
printf(“%s”,sentence);
fclose(f);
return(0);
}
Output
Contents of file are=i love india




9. fprintf() function

fprintf()  is a predefined function of C language which is used to write  different types of data to a file. This function is similar to printf(), except that a FILE pointer is included as the first argument.

Like printf(), we can format the data in a variety of ways by using fprintf(). This function is used to insert formatted data into a file. The syntax of fprintf() function is:

fprintf(Filepointer,”Control String”,Arg1,Arg2,Arg3,….);

Filepointer refers to the file pointer variable which is used to refer to a file.

Control string can contain various conversion characters such as %d, %f, %s to insert different types of data  into a file.

Arg1,Arg2,Arg3,…. are known as arguments. They represent various values that must be fed into the file.

 Program to insert data into a file using fprintf() function
#include<stdio.h>
int main()
{
FILE *f;
int rollno;
char name[20];
f=fopen(“student”,”w”);
printf(“Enter rollno of student:”);
scanf(“%d”,&rollno);
printf(“Enter name of student:”);
fflush(stdin);
gets(name);
fprintf(f,”%d%s”,rollno,name);
fclose(f);
return(0);
} 
Output
Enter rollno of student:1
Enter name of student:Amit

10. fscanf() function

fscanf()  is a predefined function of C language which is used to read different types of data from a file. This function is similar to scanf(), except that a FILE pointer is included as the first argument.

Like scanf(), we can read and store the data into variables as per the format specified in fscanf().The fscanf() function works exactly like the scanf() function except that it reads the information from the stream specified by File pointer instead of standard input device.

The syntax of fprintf() function is:

fscanf(Filepointer,”Control String”,Arg1,Arg2,Arg3,….);

Filepointer refers to the file pointer variable which is used to refer to a file.

Control string can contain various conversion characters such as %d, %f, %s to read different types of data from file.

Arg1,Arg2,Arg3,…. are known as arguments. These are various variables which will contain values read from the file. 

Program to read data from a file using fscanf() function.
#include<stdio.h>
int main()
{
FILE *f;
int rollno;
char name[20];
f=fopen(“student”,”r”);
fscanf(f,”%d%s”,&rollno,name);
printf(“\nRollno of student=%d”,rollno);
printf(“\nName of student=%s”,name);
fclose(f);
return(0);
}
Output
Rollno of student=1
Name of student=Amit 

11. fwrite() function

fwrite() is a predefined function of C which is used to write a block of data into a file rather than individual elements. Each block has some fixed size. It may be a simple variable, an array or variable of a structure.

It is easy to write the entire block to a file. fwrite() is mainly used for binary files or binary modes. The syntax of fwrite() function is:

fwrite(Object,Size,N,Filepointer);

Object refers to a simple variable, an array or a structure type variable which we want to write into a file.

Size refers to the size of data which we want to write into file.

n refers to the number of items to be written to the file.

Filepointer refers to the file pointer variable which is used to refer to a file in which we want to write the data.

 Program to write data into a file using fwrite() function.
#include<stdio.h>
struct student
{
int rollno;
char name[20];
};
int main()
{
FILE *f;
struct student s;
f=fopen(“data”,”wb”);
printf(“Enter rollno=”);
scanf(“%d”,&s.rollno);
printf(“Enter name=”);
fflush(stdin);
gets(s.name);
fwrite(&s, sizeof(s), 1, f);
fclose(f);
printf(“File created..”);
return(0);
}
Output
Enter rollno=9
Enter name=rahul
File created..

12.  fread() function

fread() is a predefined function of C which is used to read a block of data from a file rather than individual elements. Each block has some fixed size. It may be a simple variable, an array or variable of a structure.

It is easy to read the entire block from a file. fread() is mainly used for binary files or binary modes. The syntax of fread() function is:

fread(Object,Size,N,Filepointer);

Object refers to a simple variable, an array or a structure type variable which we want to read from a file.

Size refers to the size of data which we want to read from a file.

n refers to the number of items to be read from the file.

Filepointer refers to the file pointer variable which is used to refer to a file from which we want to read the data. 

Program to read data from a file using fread() function.
#include<stdio.h>
struct student
{
int rollno;
char name[20];
};
int main()
{
FILE *f;
struct student s;
fread(&s, sizeof(s), 1, f);
printf(“\nRollno=%d”,s.rollno);
printf(“\nName=%s”,s.name);
fclose(f);
return(0);
}
Output
Rollno=9
Name=rahul



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