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
Ad

More Related Content

What's hot (20)

File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil Dutt
 
File in c
File in cFile in c
File in c
Prabhu Govind
 
File handling in c
File handling in cFile handling in c
File handling in c
mohit biswal
 
File operations in c
File operations in cFile operations in c
File operations in c
baabtra.com - No. 1 supplier of quality freshers
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
File handling in c
File handling in c File handling in c
File handling in c
Vikash Dhal
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
Unit5
Unit5Unit5
Unit5
mrecedu
 
File handling in c
File  handling in cFile  handling in c
File handling in c
thirumalaikumar3
 
File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
Satz1
Satz1Satz1
Satz1
rajeshmhvr
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
thirumalaikumar3
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
file
filefile
file
teach4uin
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
File handling in C
File handling in CFile handling in C
File handling in C
Rabin BK
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 

Similar to Data Structure Using C - FILES (20)

want to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learnwant to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learn
nalluribalaji157
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
 
Unit 8
Unit 8Unit 8
Unit 8
Keerthi Mutyala
 
file handling in c programming with file functions
file handling in c programming with file functionsfile handling in c programming with file functions
file handling in c programming with file functions
10300PEDDIKISHOR
 
File handling
File handlingFile handling
File handling
Ans Ali
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
DHARUNESHBOOPATHY
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
arnold 7490
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
MugdhaSharma11
 
pre processor and file handling in c language ppt
pre processor and file handling in c language pptpre processor and file handling in c language ppt
pre processor and file handling in c language ppt
shreyasreddy703
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
aarockiaabinsAPIICSE
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
file handling1
file handling1file handling1
file handling1
student
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
want to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learnwant to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learn
nalluribalaji157
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
 
file handling in c programming with file functions
file handling in c programming with file functionsfile handling in c programming with file functions
file handling in c programming with file functions
10300PEDDIKISHOR
 
File handling
File handlingFile handling
File handling
Ans Ali
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
MugdhaSharma11
 
pre processor and file handling in c language ppt
pre processor and file handling in c language pptpre processor and file handling in c language ppt
pre processor and file handling in c language ppt
shreyasreddy703
 
file handling1
file handling1file handling1
file handling1
student
 
Ad

Recently uploaded (20)

Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
Quiz Club of PSG College of Arts & Science
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docxPeer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
19lburrell
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docxPeer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
19lburrell
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Ad

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
  翻译: