SlideShare a Scribd company logo
Data
Structure
Using C
FILES
DR. HARISH KAMAT. H.O.D.
KWT’S DIVEKAR B.C.A. COLLEGE,
KARWAR.
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.
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.
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.
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.
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
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.
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.
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.
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
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
File Operations
Writing data into the File
Writing into the file is supported by the below function:
putc()
putw()
fprintf()
BACK
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);
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
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);
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).
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
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”);
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.
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
File Operations
Reading data from the File
Reading from the file is supported by the below function:
getc()
getw()
fscanf()
BACK
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);
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
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);
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.
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
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);
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);
}
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
Error Handling
Beyond end of the file.
Unable to open a file.
Invalid file name.
Mode of the file is different.
Error Handling Function
Following functions support to handle the errors:
feof()
ferror()
BACK
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.
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
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.
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
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
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.
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
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.
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.
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;
}
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
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

More Related Content

PPTX
File Management in C
PPT
PPTX
File management
PPT
File handling in 'C'
DOCX
Understanding c file handling functions with examples
PDF
Module 03 File Handling in C
PPSX
C programming file handling
PDF
Files in C
File Management in C
File management
File handling in 'C'
Understanding c file handling functions with examples
Module 03 File Handling in C
C programming file handling
Files in C

What's hot (20)

PPT
File handling-dutt
PPT
File in c
PPTX
File handling in c
PPTX
File in C language
PPTX
C Programming Unit-5
PPT
File handling in c
PPT
File in C Programming
PPTX
File handling in C
PPTX
File handling in c
PPT
Unit5
PPT
File handling in c
PPT
File Management
DOCX
Satz1
PPT
File handling-c programming language
PPT
File handling in c
PPT
file
PPTX
File handling in C by Faixan
PPTX
File handling in C
DOCX
Unit 5 dwqb ans
File handling-dutt
File in c
File handling in c
File in C language
C Programming Unit-5
File handling in c
File in C Programming
File handling in C
File handling in c
Unit5
File handling in c
File Management
Satz1
File handling-c programming language
File handling in c
file
File handling in C by Faixan
File handling in C
Unit 5 dwqb ans
Ad

Similar to Data Structure Using C - FILES (20)

PPTX
want to learn files,then just use this ppt to learn
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
DOCX
PPTX
file handling in c programming with file functions
PPT
File handling
PPT
File_Handling in C.ppt
PPT
File_Handling in C.ppt
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPT
file_handling_in_c.ppt
PPT
Unit5 C
PPT
Lecture 20 - File Handling
PPT
file_handling_in_c.ppt
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PPTX
Concept of file handling in c
PPTX
pre processor and file handling in c language ppt
PPTX
5 Structure & File.pptx
PPTX
Unit-VI.pptx
PPTX
PPS PPT 2.pptx
PPT
file handling1
PDF
Module 5 file cp
want to learn files,then just use this ppt to learn
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
file handling in c programming with file functions
File handling
File_Handling in C.ppt
File_Handling in C.ppt
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
file_handling_in_c.ppt
Unit5 C
Lecture 20 - File Handling
file_handling_in_c.ppt
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Concept of file handling in c
pre processor and file handling in c language ppt
5 Structure & File.pptx
Unit-VI.pptx
PPS PPT 2.pptx
file handling1
Module 5 file cp
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
master seminar digital applications in india
Abdominal Access Techniques with Prof. Dr. R K Mishra
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
TR - Agricultural Crops Production NC III.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Data Structure Using C - FILES

  • 1. Data Structure Using C FILES DR. HARISH KAMAT. H.O.D. KWT’S DIVEKAR B.C.A. COLLEGE, KARWAR.
  • 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.
  • 31. Error Handling Function Following functions support to handle the errors: feof() ferror() BACK
  • 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