2. Topics Covered
▪ Files & Its Types
▪ Operations on File
▪ Sequential Files
▪ Random Files
3. Files & Its Types
Collection of information.
Types of files
1. Sequential files
2. Random files
1. Sequential file: data is accessed one after the
another Eg: Cassettes
2. Random file : data can be accessed from
anywhere Eg: CD
3
4. Operations on File
1.Read
2.Write
3.Append
In order to perform any operations we require to do
following task:
1. Declare File pointer
2. Open File
3. Perform operation
4. Close File
4
5. Continue..
Declaration of File:
FILE *Pointer_to_File;
Opening of File
Pointer_to_File= fopen(filename, mode);
Modes indicates operation on File:
r -> read
w -> write
a ->append
w+ -> readwrite -> destorys previous data
r+ -> readwrite -> fetches an error if it doesnot exist
a+ -> readwrite -> written at end of the file
Closing of File:
fclose(Pointer_to_File);
5
6. Sequential File
In order to perform operation we require functions to read or write data.
1. Writing data to Files:
a. int fprintf(pointer_to_file,”control string”, varlist);
b. int fputc(character, pointer_to_file );
c. int fputs(string, pointer_to_file );
2. Reading data from Files:
a. int fscanf(pointer_to_file,”control string”, varlist);
b. int fgetc(pointer_to_file);
c. char * fgets(string,string_size,pointer_to_file);
6
7. Writing data Using fprintf()
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables INPUT
File *fptr;
int age;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”w”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0); OUTPUT
}
// Get input from user
printf(“enter name and age”);
scanf(“%s%d”, &name, &age);
// write into file
fprintf(fptr,“%s%d”, name, age);
fclose(fptr);
getch();
}
7
Console Window:
enter name and age
abc
18
program1.c
abc 18
8. Writing data Using fputc() & fputs()
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables
File *fptr;
char ch;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”w”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
// Get input from user
printf(“enter name and character”);
scanf(“%s%c”, &name, &ch);
fputs(name, fptr);
fputc(ch, fptr);
fclose(fptr);
getch();
}
8
Console Window:
enter name and character
abc
$
program1.c
abc $
9. Reading from File
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables
File *fptr;
int age;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”r”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
//Reading from file
fscanf(fptr,“%s%d”, &name, &age);
printf(“contents from file %s %d”,name,age);
fclose(fptr);
getch();
}
9
Console Window:
contents from file
abc 19
program1.c
abc 19
10. Continue..#include<stdio.h>
#include<conio.h>
void main()
{ Input
//Declaration of variables
File *fptr;
char ch;
clrscr();
//open file
fptr=fopen(fptr,”program2.c”,”r”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
ch= fgetc(fptr);
while(ch!=EOF)
{
printf(“%c”, ch);
ch= fgetc(fptr);
}
fclose(fptr);
getch();
}
10
Console Window:
a
program2.c
a
program2.c
b
Console Window:
ab
program2.c
ab
12. Random Files
Functions used:
1. int fseek(File *stream, long offset, int whence)
▪ File pointer
▪ Number of bytes
▪ Position
2. int ftell()
3. rewind()
12
Beginning 0 or SEEK_SET
Current 1 or SEEK_CUR
End 2 or SEEK_END