Академический Документы
Профессиональный Документы
Культура Документы
INTRODUCTION
Computer stores programs and data in secondary memory in the form of files.
Storing programs and data in primary memory is not preferred due to its volatility
nature which loses its contents when the power goes off.
Interaction between program console and file
Secondary
memory Data
files
Write data
Program-file
Read data
Primary memory
Programs+D
ata
Write data
Program-
o/p
screen
File
Types of Files
Text Files
Binary Files
Text Files
They are the files that contains letters, digits and symbols. These Files contains
the characters coded from ASCII Character set.
Binary Files
It is sequential access file in which data are stored and read back one after the
another in binary format instead of ASCII format.
Operations on Files
The common operations that can be performed on files are
Naming File
Naming of File
The file name must be specified for particular file. The file name is string of
characters. The valid file names are
File1.txt, File2.c, File3.h, File4.dat and so on.
If the file is used for storing text .txt extension is used. If the file stores C
program .c extension is used. If the file is header file .h extension is used.
Opening and Closing of Files
fopen()
The function fopen() is used for opening file. Every File that is opened returns a
pointer to particular file. The syntax is given below
fptr=fopen(filename or path of file,mode);
filename or path of file is the name of file being opened. If path of file is not
mentioned file exists in current directory.
Eg: If you are working in C:\Tc and if the path of file is not mentioned then file exists
in C:\Tc directory.
r+
w+
a+
Binary Files
rb
wb
ab
rb+
wb+
ab+
fptr is pointer variable that contains the address of structure FILE that has been
defined in stdio.h header file.
Eg
FILE *fptr;
fptr=fopen(text.txt,r);
the text.txt file is opened for reading only.
fclose()
fclose() is used for closing file which is being opened. The syntax is
fclose(fptr);
Eg:
FILE *fptr;
Fptr=fopen(text.txt,r);
/* perform operations on file */
fclose(fp);
Write data to file if data does not exist manually or using program
Program
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("text.txt","r"); /* opens file for reading */
ch=fgetc(fp);/* read character from file and assign it to ch */
printf("%c",ch);
/* Loop until the end of file is reached */
while(ch!=EOF)
{
ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
}
O/P
fputc()
It is used for writing character to file. The syntax is as follows
fputc(ch,fptr);
Program
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("text.txt","w"); /* opens file for writing */
/* when the file is opened in write mode in already existing file
the data in previous file will be overwritten with new data */
ch=getchar();/* read character from keyboard inorder to write data to file*/
/* Loop until the end of file is reached */
while(ch!=EOF)
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
After writing content to file contents of text.txt file are overwritten with
new data
After compiling and running the above program new file text1.txt will be created.
Appending the data to file say text1.txt which is already existing in current directory
and data in that file is not empty.
Program
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("text1.txt","a"); /* opens file for appending data at end */
/* when the file is opened in append mode in already existing file
the data will be added to the previous file at the end */
ch=getchar();/* read character from keyboard inorder to append data to file*/
/* Loop until the end of file is reached */
while(ch!=EOF)
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
FILE *fp;
char str[10];
int n,i;
clrscr();
fp=fopen("text2.txt","w"); /* opens file for writing */
/* when the file is opened in write mode in already existing file
the data in previous file will be overwritten with new data */
printf("enter number of students");
scanf("%d",&n);
fflush(stdin);/* remove the previous data from input buffer */
/* loop through the number of students to enter names of students
to write data to file */
for(i=1;i<=n;i++)
{
gets(str);
fputs(str,fp);
}
fclose(fp);
}
fgets()
This function is used to read string from file upto its specified size. The
syntax of fgets() is given below
fgets(buffer,size,fptr);
buffer is name of character array,size is integer value, fptr is pointer to FILE
type. This function reads string of maximum size-1 characters from file pointed to
by fptr and copies data to the memory area denoted by buffer.
Example Explanation of fgets()
Step 1: Opens the file in read mode.
Step 2: Read the contents from file upto its maximum size.
Step 3: Close the file.
Program
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char str[10];
int size;
clrscr();
fp=fopen("text2.txt","r"); /* opens file for reading string from file */
printf("enter size");
scanf("%d",&size);
fgets(str,size,fp);/* reads string of size -1 characters from file pointed
to by fp and store data in str */
puts(str);/* prints the string contents */
fclose(fp);
}
O/P
fscanf()
This function is used to read multiple data items which may be of different
types from file. The syntax is given below
fscanf(fptr,control string,arguments list);
O/P
struct student s;
FILE *fp;
int i,n;
clrscr();
fp=fopen("text4.txt","wb"); /* opens binary file for writing structure
of data*/
printf("enter the number of records");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%s",&s.rollno,&s.name);/* scan student details */
fwrite(&s,sizeof(s),1,fp);/* write structure to file */
}
fclose(fp);
}
Input from keyboard to write structure block to file
fread()
This function is used for reading structure block from file. The syntax is
given below.
fread(&structure-variable,sizeof(structure),integer,fptr);
The first argument is address of structure to be read from disk, the second
argument specifies size of structure in bytes, integer is number of records
and fptr is pointer to file type.
Example Explanation of fread()
Program
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[100];
};
main()
{
struct student s;
FILE *fp;
clrscr();
fp=fopen("text4.txt","rb"); /* opens binary file for reading structure
block from file*/
while(fread(&s,sizeof(s),1,fp)) /* read strucure data from file till
there is no content in file */
{
printf("%d%s",s.rollno,s.name);
}
fclose(fp);}
O/P
fseek()
ftell()
rewind()
fseek()
This function is used for setting pointer position in file at specified
position. The syntax is given below
fseek(fp,displacement,pointer_position);
fp is pointer to FILE type. Displacement is positive or negative. This is
number of bytes which are skipped backward(if negative) or forward(if
positive) from current pointer position. Pointer_position can take one of 3
values 0,1,2. The meanings of values are given below.
Pointer_position
0
1
2
Macro name
SEEK_SET
SEEK_CUR
SEEK_END
Meaning
Beginning of file
Current position of file
End of file
Eg:
fseek(fp,0,1)-Stay at the current position
fseek(fp,n,1)-move pointer forward by n bytes
fseek(fp,-n,1)-go backward by n bytes from current position.
Program
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("text1.txt","r"); /* opens file for reading*/
fseek(fp,5,1);
printf("File pointer position is %d",ftell(fp));
printf("\n");
while(!feof(fp))
{
ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
O/P
feof()
This function avoids reading file after the end of file mark.
Syntax
feof(fp)
Example Explanation
Trying to open file which doesnot exist in current directory
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("text5.txt","r");
if(fp==NULL)
{
printf("The file doesnot exist");
exit(1);
}
fclose(fp);
}
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fp,*fp1;
char ch;
clrscr();
fp=fopen("text1.txt","r");
if(fp==NULL)
{
printf("The file doesnot exist");
exit(1);
}
fclose(fp);
fp1=fopen("text2.txt","r");
fputc('c',fp1);
if(ferror(fp1))
{
printf("The file is opened in read mode.it is not used for writing");
exit(1);
}
fclose(fp1);
}
O/P