This video has covered FILES chapter in Data Structure using C for BCA II semester, as per syllabus prescribed by Karnatak University , Dharwad,Karnataka,INDIA.
2. Outline
Introduction.
Naming a File.
Opening a File.
Closing a File.
File Operations.
Writing data into the File.
Reading Data from the File.
Error Handling.
Random Access to File.
3. Introduction
Files are the containers of relative data in the computer memory.
File is a data type is C language, called empty data set.
This data structure is defined in the header file called stdio.h
This is data type takes any type of data.
4. 1. What happens with the data after the
termination of program execution?
Yes, you are correct , It is lost or discarded.
2. If lost so, then during program execution where
those data are stored?
Yes, again you are correct , It is stored in RAM
memory.
5. continued….
Data type is FILE.
Declaration along with pointer variable.
FILE *p , *ptr ; etc
File Pointer variable by default it points to the first character of the mentioned file.
6. Naming a File
Relevant name should be used.
Name of the file is general word. No rules are applied while naming a file.
Example :
Admission2020.txt StaffSalary.txt etc.
BACK
7. Creating or Opening a File
To create or open a file , purpose of the file.
Function fopen();
General Syntax :
FILE *fp=fopen(“<abc>”, “<xyz>”);
where,
fp - valid pointer variable holds the file pointer to abc.
abc - name of the file.
xyz - purpose or Mode of the file.
The function fopen(), returns a file pointer on successful of creating a file, & on
unsuccessful it returns null pointer.
8. MODE OF FILE
Mode is a character constant which tells the compiler, what is the purpose of the file.
MODES in fopen()
Character Constant Purpose
r Read only.
w write only.
a append.
r+ Read & Write.
w+ Read & Write.
a+ Read, Write & Append.
9. Closing a FIle….
fclose(); , is a function to close the opened file.
General syntax :
fclose(“<abc>”);
where,
fclose() - taken one parameter i.e file pointer variable.
abc - file pointer.
10. Sample C program to Open & Close File
#include<stdio.h>
main()
{
FILE *ptr;
ptr=fopen(“testOpenClose.txt", "w");
if(ptr!=NULL)
printf("FILE IS CREATEDn");
else
printf("FILE COULD NOT CREATEDn" );
fclose(ptr);
}
Note: upon successful execution, you get a file testOpenClose.txt in the same
location where program is stored
11. Assignment 1
1. Write a C-Program to create a file with the name entered by the user.
( Description: during execution of the program, read the name of the file
from the user and create file with the same name. Display the result.)
BACK
12. File Operations
Writing data into the File
Writing into the file is supported by the below function:
putc()
putw()
fprintf()
BACK
13. putc()
This function take a single character and writes into the specified file.
General Syntax:
putc(‘<character>’,<ptr>);
where,
putc() - takes two parameter.
character - any ASCII character.
ptr - file pointer variable.
EXAMPLE:
FILE *ptr= fopen(“test.txt", "w");
putc(‘a’,ptr);
14. Sample C Program illustrating putc()
#include<stdio.h>
main()
{
FILE *ptr;
ptr=fopen(“testPutc.txt", "w");
if(ptr!=NULL)
putc('a',ptr);
printf("DATA IS WRITTEN INTO THE FILE");
fclose(ptr);
}
Note: upon successful execution, you get a file testPutc.txt in the same location
where program is stored with the data written.
BACK
15. putw()
This function take numeric data and writes into the specified file.
General Syntax:
putw(‘<numeric>’,<ptr>);
where,
putw() - takes two parameter.
numeric- any numeric value.
ptr - file pointer variable.
EXAMPLE:
FILE *ptr= fopen(“testPutw.txt", "w");
putw(50,ptr);
16. Sample C Program illustrating putw()
#include<stdio.h>
main()
{
FILE *ptr;
ptr=fopen(“testPutw.txt", "w");
if(ptr!=NULL)
putw(50,ptr);
printf("DATA IS WRITTEN INTO THE FILE");
fclose(ptr);
}
Note: upon successful execution, you get a file testPutw.txt in the same location where
program is stored with the data written(directly may not be read).
17. Assignment 2 & 3
2. Write a C-Program to create a file & write a character.
( Description: during execution of the program, read name of the file & a
character from the user and write into the file. Display the result.)
3. Write a C-Program to create a file & write a numeric value.
( Description: during execution of the program, read name of the file & a
numeric value from the user and write into the file.Display the result.)
BACK
18. fprintf()
This is formatted input function, supports to input any type of data into the file.
Multiple types of data can be written into the file at once.
General Syntax:
fprintf(<ptr>, “Format Specifier”, “<comma separated data/ variables>”);
where,
ptr - file pointer variable.
format Specifier - %c %d %f %lf %s
Variables - valid variable which has got data.
Example:
fprintf(ptr, “ %d %s %c”, 28, “prateek”, “M”);
19. Contined…..
#include<stdio.h>
main()
{
FILE *ptr;
int rno=1017;
char gender='m', name[10]="Rakesh";
ptr=fopen("testFprintf.txt", "w");
if(ptr!=NULL)
{
fprintf(ptr,"%d %s %c", rno, name, gender );
printf("DATA IS WRITTEN INTO THE FILE");
}
fclose(ptr);
}
Note: upon successful execution, you get a file testFprintf.txt in the same location where
program is stored with the data written.
20. Assignment 4
4. Write a C-Program to create a file & write a Roll number, Name and Class of a
student.
( Description: during execution of the program, read file name & data
from the user and write into the file. Display the result.)
BACK
21. File Operations
Reading data from the File
Reading from the file is supported by the below function:
getc()
getw()
fscanf()
BACK
22. getc()
This function is used to read a character from the file.
General Syntax:
<char>= getc(<ptr>);
where,
char - character type of Variable.
ptr - File Pointer variable.
Example: char ch;
ch=getc(ptr);
23. Sample C-program to illustrate getc()
#include<stdio.h>
main()
{
FILE *ptr;
char ch;
ptr=fopen("testPutc.txt", "w");
if(ptr!=NULL)
putc('a',ptr);
printf("DATA IS WRITTEN INTO THE FILEn");
fclose(ptr);
//changing the mode of the file to read
ptr=fopen("testPutc.txt", "r");
ch=getc(ptr);
printf("%c IS READ FROM THE FILE ",ch);
fclose(ptr);
}
BACK
24. getw()
This function is used to read numeric data from the specified file.
General Syntax:
<variable>=getw(<ptr>);
where,
variable - integer type of variable.
ptr - file pointer variable.
Example: int rno;
rno=getw(ptr);
25. Sample C-program to illustrate getw()
#include<stdio.h>
main()
{
FILE *ptr;
int rno;
ptr=fopen("testGetw.txt", "w");
if(ptr!=NULL)
putw(1017,ptr);
printf("DATA IS WRITTEN INTO THE FILEn");
fclose(ptr);
//changing the mode of the file to read
ptr=fopen("testGetw.txt", "r");
rno=getw(ptr);
printf("Roll number %d IS READ FROM THE FILE ",rno);
fclose(ptr);
}
Note: upon successful execution, you get a file testGetw.txt in the same location where
program is stored with the data written.
26. Assignment 5 & 6
5. Write a C-Program to create a file & write a character.
( Description: during execution of the program, read file name & a
character from the user and write into the file. Display the result.)
6. Write a C-Program to create a file & write a numeric value.
( Description: during execution of the program, read file name & a
numeric value from the user and write into the file. Display the result.)
BACK
27. fscanf()
This is formatted output function, supports to read any type of data from the file.
Multiple types of data can be read from the file at once.
General Syntax:
fscanf(<ptr>, “Format Specifier”, “<comma separated variable address>”);
where,
ptr - file pointer variable.
format Specifier - %c %d %f %lf %s
Variables - address of the variable which will get data from the file.
Example: int rno; char name[10], gender;
fscanf(ptr, “ %d %s %c”, &rno,name,gender);
28. Sample C-program to illustrate fscanf()
#include<stdio.h>
main()
{
char name[10]="RAJAT";
int rno=1017;
FILE *ptr;
ptr=fopen("testfScanf.txt","w");
fprintf(ptr,"%d%s",rno,name);
printf("DATA IS WRITTEN INTO THE FILEn");
fclose(ptr);
//reopening file with read mode
ptr=fopen("testfScanf.txt","r");
// reading data from the file
fscanf(ptr,"%d%sn",&rno,name);
printf("FILE DATA ISn");
printf("ROLL NO t NAMEn");
printf("%dtt%sn",rno,name);
fclose(ptr);
}
29. Assignment 7
4. Write a C-Program to create a file, read Roll number, Name and Marks Scored in
two subjects of a student & display the same.
( Description: during execution of the program, read file name & data
from the user. Write those data into the file & read data from the file.
Display the result. )
BACK
30. Error Handling
Beyond end of the file.
Unable to open a file.
Invalid file name.
Mode of the file is different.
32. Error Handling
feof()
This function is used to check the end-of-the-file.
It takes one parameter of file pointer variable and returns an integer value.
non-zero.
zero.
General Syntax:
foef(<ptr>);
where,
ptr - file pointer variable.
33. SAMPLE c-Program to illustrate feof()
# include <stdio.h>
int main( )
{
FILE *fp ;
char ch;
fp=fopen("testFeof.txt","w");
putc('K',fp);
fclose ( fp );
fp = fopen ( "testFeof.txt", "r" ) ;
if ( fp == NULL )
{
printf ( "nCOULD NOT OPEN THE FILE
testFeof.txtn") ;
return 1;
}
#printf( "nREADING THE FILE testFeof.txtn" ) ;
while ( 1 )
{
ch = getc ( fp ) ; // reading the file
if( feof(fp) )
break ;
printf ( "%c", ch ) ;
}
printf("n CLOSING THE FILE testFeof.txt AS END OF
THE FILE IS REACHED");
// Closing the file
fclose ( fp ) ;
return 0;
}
BACK
34. Error Handling
Ferror()
This function is used to check the status of the file.
File exists or not.
Mode of the file.
General Syntax:
ferror(<ptr>)
where,
ferror() - returns, non-zero if error is detected otherwise zero.
35. Assignment 8 & 9
8. Write a C program to create a file, read & store N numbers. Using this file data, create
two more files.
(description: create a file say DATA.txt, store N numbers read from the user. Create
two more files which consists of even and odd numbers, data should be read from
DATA.txt. Display the result.)
9. Write a C program to create a file, read and store Roll No, name & marks of a student.
(Description: create a file to read data from the user. Calculate total and percentage
using marks scored in 3 subject. Display the result.)
BACK
36. Random Access to the File
Sequential access to file means, accessing from the beginning of the file i.e read & write
sequentially.
Random access to file allows us to access data any location from the file.
Function supports to Random access to the file:
fseek
ftell
rewind
BACK
37. fseek()
fseek() function is used to move file pointer position to the given location.
General Syntax:
fseek(<fp>, <offset>, <start_point>);
where,
fp - file pointer.
offset - Number of bytes/characters to be offset/moved from.
Start_point - the current file pointer position. This is the current file
pointer position from where offset is added.
fseek - returns zero on success otherwise non-zero.
38. Sample C-program to illustrate fseek()
#include <stdio.h>
int main ()
{
FILE *ptr;
char data[44];
ptr = fopen ("testFseek.txt","w");
fprintf(ptr,"%s","WEL COME TO
DIVEKAR BCA VIDEO TUTORIAL
CLASS");
fclose(ptr);
ptr = fopen ("testFseek.txt","r");
fgets ( data, 45, ptr );
printf("Before fseek - %s n", data);
// To set file pointet to 12th
byte/character in the file
fseek(ptr, 12, 0);
fgets ( data, 44, ptr );
printf("nAFTER START
POINT SET TO 12 IS : %s",
data);
fclose(ptr);
return 0;
}
BACK
39. ftell()
This function is used to get the current position of the file pointer .
General Syntax:
<integer>=ftell(<ptr>);
where,
integer - long integer type of variable.
ptr - file pointer variable.
ftell() - takes a parameter to file pointer and returns long integer
value.
40. Rewind()
rewind function is used to move file pointer position to the beginning of the file.
General Syntax:
rewind(<ptr>);
where,
ptr - file pointer variable.
rewind() - takes one parameter to file pointer.
41. Sample C-Program to illustrate ftell() and
rewind()
#include <stdio.h>
int main ()
{
FILE *ptr;
ptr = fopen ("test.txt","w");
printf("POINTER IS AT %dn",ftell(ptr));
fprintf(ptr,"%s","WEL COME TO DIVEKAR
BCA VIDEO TUTORIAL CLASS");
fclose(ptr);
BACK
ptr = fopen ("test.txt","r");
fseek(ptr, 12, 0);
printf("AFTER SEEK POINTER IS AT
%dn",ftell(ptr));
rewind(ptr);
printf("AFTER REWIND POINTER IS AT
%dn",ftell(ptr));
fclose(ptr);
return 0;
}
42. Assignment 10
10 . Write a C program to create a file, read and store Roll No, name & marks of N
student.
(Description: create a file to read data from the user. Calculate total and percentage
using marks scored in 3 subject. Display the result.)
BACK
43. BACK
Introduction.
Naming a File.
Opening a File.
Closing a File.
File Operations.
Writing data into the File.
Reading Data from the File.
Error Handling.
Random Access to File.
Data Structure Using C- FILES