Working With Files
Working With Files
Working With Files
Declaration:
int fprintf(FILE *fp, const char *format, …);fprintf()
fprintf() – To write into a function writes string into a file pointed by fp. In a C
file program, we write string into a file as below.
fprintf (fp, “some data”); or
fprintf (fp, “text %d”, variable_name);
File Processing Techniques
Depending up on the method of accessing the data stored ,there are two types of
files.
• Sequential file
• Random access file
1.Sequential File: In this type of files data is kept in sequential order if we want to
read the last record of the file,we need to read all records before that record so it
takes more time.
2.Random access Files: In this type of files data can be read and modified randomly
.If we want to read the last record we can read it directly.It takes less time when
compared to sequential file.
rewind()
The C library function void rewind(FILE *stream) sets the file position to the
beginning of the file of the given stream.
Declaration
• Following is the declaration for rewind() function.
• void rewind(FILE *stream)
Parameters
• stream − This is the pointer to a FILE object that identifies the stream.
fseek()
The C library function int fseek(FILE *stream, long int offset, int whence) sets the
file position of the stream to the given offset.
Declaration
• Following is the declaration for fseek() function.
• int fseek(FILE *stream, long int offset, int whence)
Parameters
• stream − This is the pointer to a FILE object that identifies the stream.
• offset − This is the number of bytes to offset from whence.
• whence − This is the position from where offset is added. It is specified by one
of the following constants −
#include <stdio.h>
int main () {
FILE *fp;
fp = fopen("file.txt","w+");
fputs("This is tutorialspoint.com", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Language", fp);
fclose(fp);
return(0);
}
ftell() in C
• In C language, ftell() returns the current file position of the specified stream
with respect to the starting of the file. This function is used to get the total size
of file after moving the file pointer at the end of the file. It returns the current
position in long type and file can have more than 32767 bytes of data.
Here is the syntax of ftell() in C language,
long int ftell(FILE *stream)
Here is the parameter used in ftell(),
• stream − This is the pointer to a FILE object that identifies the stream.
#include <stdio.h>
#include<conio.h>
void main () {
FILE *f;
int len;
f = fopen("one.txt", "r");
if(f == NULL) {
perror(“Error opening file”);
return(-1);
}
opening read write and appending on from data file in c
The character I/O
fgetc()
• fgetc() is used to obtain input from a file single character at a time. This function
returns the ASCII code of the character read by the function. It returns the
character present at position indicated by file pointer. After reading the
character, the file pointer is advanced to next character. If pointer is at end of file
or if an error occurs EOF file is returned by this function.
fputc()
• fputc() is used to write a single character at a time to a given file. It writes the
given character at the position denoted by the file pointer and then advances the
file pointer.
This function returns the character that is written in case of successful write
operation else in case of error EOF is returned.
#include<stdio.h>
#include<conio.h>
void main() {
FILE *f;
char s;
clrscr();
f=fopen("new.txt","r");
while((s=fgetc(f))!=EOF) {
printf("%c",s);
}
fclose(f);
getch(); }
#include
<stdio.h>
void main() {
FILE *f;
f = fopen("new.txt", "w");
fputc('a',f);
fclose(f);
}
The program will modify the “new.txt” file. It will not display any output to the
screen but it will modify the file directly. You can check the modified file.
The string I/O
The fputs() function writes a line of characters into file. It outputs string to a
stream.
int fputs(const char *s, FILE *stream)
The fgets() function reads a line of characters from file. It gets string from a stream.
char* fgets(char *s, int n, FILE *stream)
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
clrscr();
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
clrscr();
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
getch();
}
The formatted I/O
The fprintf() function is used to write set of characters into file. It sends formatted
output to a stream.
The fscanf() function is used to read set of characters from file. It reads a word
from the file and returns EOF at the end of file
#include <stdio.h>
main(){
FILE *fp;
char buff[255];//creating char array to store data of file
fp = fopen("file.txt", "r");
while(fscanf(fp, "%s", buff)!=EOF){
printf("%s ", buff );
}
fclose(fp);
}
#include <stdio.h>
main(){
FILE *fp;
fp = fopen("file.txt", "w");//opening file
fprintf(fp, "Hello file by fprintf...\n");//writing data into file
fclose(fp);//closing file
}
Record Block I/O
• It is useful to store the block of data into the file rather than individual
elements.Each block has some fixed size,it may be of strcture or of an array.It is
possible that a data file has one or more structures or arrays,So it is easy to read
the entire block from file or write the entire block to the file.There are two
useful functions for this purpose
1. fwrite():
• This function is used for writing an entire block to a given file.
Write a program to read an employee details and write them into the file at a
time using fwrite().
#include<stdio.h>
#include<conio.h>
void main() {
struct emp {
int eno;
char ename[20];
float sal;
}e;
FILE *fp;
fp=fopen("emp.dat", "wb");
clrscr();
printf("Enter employee number");
scanf("&d",&e.eno);
printf("Enter employee name");
fflush(stdin);
scanf("%s",e.ename);
printf("Enter employee salary");
scanf("%f",&e.sal);
fwrite(&e,sizeof(e),1,fp);
printf("One record stored successfully");
getch();
}
2. fread():
• This function is used to read an entire block from a given file.
• Syntax: fread ( ptr , size , nst , fptr);
Where ptr is a pointer which points to the array which receives structure.
Size is the size of the structure
nst is the number of the structure
fptr is a filepointer.
Write a program to read an employee details at a time from a file using fread()
which are written above using fwrite()and display them on the output screen.
#include<stdio.h>
#include<conio.h>
void main() {
struct emp
{
int eno;
char ename[20];
float sal;
}e;
FILE *fp;
fp=fopen("emp.dat", "rb");
clrscr();
if(fp==NULL)
printf("File cannot be opened");
else
fread(&e,sizeof(e),1,fp);
printf("\nEmployee number is %d",e.eno);
printf("\nEmployee name is %s",e.ename);
printf("\nEmployee salary is %f",e.sal);
printf("One record read successfully");
getch();
}
fprintf ()
• This function is same as the printf() function but it writes the data into the file,
so it has one more parameter that is the file pointer.
Syntax: fprintf(fptr, "controlcharacter",variable-names);
Where fptr is a file pointer
Control character specifies the type of data to be printed into file.
Variable-names hold the data to be printed into the file.
fscanf ()
• This function is same as the scanf() function but this reads the data from the file,
so this has one more parameter that is the file pointer.
Syntax: fscanf(fptr, "control character", &variable-names);
Where fptr is a file pointer
Control character specifies the type of data to be read from the file.
Address of Variable names are those that hold the data read from the file.
Write a program to read details of an employee and print them in a file using
fprintf() and read the same from the file using fscanf() and display on the output
screen.
#include<stdio.h>
#include<conio.h>
void main() {
FILE *fp; int eno;
char ename[20];
float sal;
clrscr();
fp=fopen("emp.dat", "w+");
printf("Enter employee number");
scanf("&d",eno);
printf("Enter employee name");
fflush(stdin);
scanf("%s",ename);
printf("Enter employee salary");
scanf("%f",sal);
fprintf(fp, "%d %s %f\n",eno,ename,sal);
printf("Details of an employee are printed into file");
rewind(fp);
fscanf(fp,"%d %s %f",&eno,ename,&esal);
printf("Details of employee are\n");
printf("\n %d %s %f", eno,ename, sal);
fclose(fp);
getch();
}
Binary Mode operations
• Binary mode deals with data records and these records can be in the form of a
single byte or several bytes. Most often structured objects are stored in binary
mode and are stored sequentially.
Binary Mode fopen flags
#1 Binary Read (rb) SYNTAX:
Opens a file in binary read mode fp=fopen("binary.dat","rb");