SlideShare une entreprise Scribd logo
1  sur  43
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

Contenu connexe

Tendances (20)

File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
File in c
File in cFile in c
File in c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File operations in c
File operations in cFile operations in c
File operations in c
 
File in C language
File in C languageFile in C language
File in C language
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File handling in c
File handling in c File handling in c
File handling in c
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unit5
Unit5Unit5
Unit5
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File Management
File ManagementFile Management
File Management
 
Satz1
Satz1Satz1
Satz1
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
File handling in c
File handling in cFile handling in c
File handling in c
 
file
filefile
file
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 

Similaire à Data Structure Using C - FILES

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.pdfsangeeta borde
 
File handling
File handlingFile handling
File handlingAns 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.pdfsudhakargeruganti
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.pptyuvrajkeshri
 
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 REDDYRajeshkumar Reddy
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in cMugdhaSharma11
 
file handling1
file handling1file handling1
file handling1student
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 

Similaire à Data Structure Using C - FILES (20)

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
 
Unit 8
Unit 8Unit 8
Unit 8
 
File handling
File handlingFile handling
File handling
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
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
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
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
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
file handling1
file handling1file handling1
file handling1
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Unit v
Unit vUnit v
Unit v
 

Dernier

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Dernier (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

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