SlideShare a Scribd company logo
1 of 25
Download to read offline
See through C
Module 3
File Handling
Tushar B Kute
http://tusharkute.com
What is a File?What is a File?
●
AA filefile is a collection of related data that ais a collection of related data that a
computers treats as a single unit.computers treats as a single unit.
●
Computers store files to secondary storage soComputers store files to secondary storage so
that the contents of files remain intact when athat the contents of files remain intact when a
computer shuts down.computer shuts down.
●
When a computer reads a file, it copies the fileWhen a computer reads a file, it copies the file
from the storage device to memory; when itfrom the storage device to memory; when it
writes to a file, it transfers data from memory towrites to a file, it transfers data from memory to
the storage device.the storage device.
●
C uses a structure calledC uses a structure called FILEFILE (defined in(defined in
stdio.hstdio.h) to store the attributes of a file.) to store the attributes of a file.
Steps in Processing a FileSteps in Processing a File
1.1. Create the stream via a pointer variable using theCreate the stream via a pointer variable using the FILEFILE
structure:structure:
FILE *p;FILE *p;
2.2. Open the file, associating the stream name with the fileOpen the file, associating the stream name with the file
name.name.
3.3. Read or write the data.Read or write the data.
4.4. Close the file.Close the file.
The basic file operations areThe basic file operations are
●
fopen - open a file- specify how its openedfopen - open a file- specify how its opened
(read/write) and type (binary/text)(read/write) and type (binary/text)
●
fclose - close an opened filefclose - close an opened file
●
fread - read from a filefread - read from a file
●
fwrite - write to a filefwrite - write to a file
●
fseek/fsetpos - move a file pointer tofseek/fsetpos - move a file pointer to
somewhere in a file.somewhere in a file.
●
ftell/fgetpos - tell you where the file pointer isftell/fgetpos - tell you where the file pointer is
located.located.
File Open ModesFile Open Modes
More on File Open ModesMore on File Open Modes
Additionally,Additionally,
●
r+ - open for reading and writing, start at beginningr+ - open for reading and writing, start at beginning
●
w+ - open for reading and writing (overwrite file)w+ - open for reading and writing (overwrite file)
●
a+ - open for reading and writing (append if file exists)a+ - open for reading and writing (append if file exists)
File OpenFile Open
●
The file open function (The file open function (fopenfopen) serves two purposes:) serves two purposes:
– It makes the connection between the physical file andIt makes the connection between the physical file and
the stream.the stream.
– It creates “a program file structure to store theIt creates “a program file structure to store the
information” C needs to process the file.information” C needs to process the file.
●
Syntax:Syntax:
filepointer=filepointer=fopen(“filename”, “mode”);fopen(“filename”, “mode”);
More OnMore On fopenfopen
●
The file mode tells C how the program willThe file mode tells C how the program will
use the file.use the file.
●
The filename indicates the system nameThe filename indicates the system name
and location for the file.and location for the file.
●
We assign the return value ofWe assign the return value of fopenfopen to ourto our
pointer variable:pointer variable:
spData = fopen(“MYFILE.TXT”, “w”);spData = fopen(“MYFILE.TXT”, “w”);
spData = fopen(“A:MYFILE.TXT”, “w”);spData = fopen(“A:MYFILE.TXT”, “w”);
More OnMore On fopenfopen
Closing a FileClosing a File
●
When we finish with a mode, we need to close the fileWhen we finish with a mode, we need to close the file
before ending the program or beginning another modebefore ending the program or beginning another mode
with that same file.with that same file.
●
To close a file, we useTo close a file, we use fclosefclose and the pointer variable:and the pointer variable:
fclose(spData);fclose(spData);
fprintf()fprintf()
Syntax:Syntax:
fprintf (fp,"string",variables);fprintf (fp,"string",variables);
Example:Example:
int i = 12;int i = 12;
float x = 2.356;float x = 2.356;
char ch = 's';char ch = 's';
FILE *fp;FILE *fp;
fp=fopen(“out.txt”,”w”);fp=fopen(“out.txt”,”w”);
fprintf (fp, "%d %f %c", i, x,fprintf (fp, "%d %f %c", i, x, ch);ch);
fscanf()fscanf()
Syntax:Syntax:
fscanf (fp,"string",identifiers);fscanf (fp,"string",identifiers);
Example:Example:
FILE *fp;FILE *fp;
fp=fopen(“input.txt”,”r”);fp=fopen(“input.txt”,”r”);
int i;int i;
fscanf (fp,“%d",&i);fscanf (fp,“%d",&i);
fgetc()fgetc()
Syntax:Syntax:
identifier = fgetc (file pointer);identifier = fgetc (file pointer);
Example:Example:
FILE *fp;FILE *fp;
fp=fopen(“input.txt”,”r”);fp=fopen(“input.txt”,”r”);
char ch;char ch;
ch = fgetc (fp);ch = fgetc (fp);
fputc()fputc()
write a single character to the output file, pointed to by fp.write a single character to the output file, pointed to by fp.
Example:Example:
FILE *fp;FILE *fp;
char ch;char ch;
fputc (ch,fp);fputc (ch,fp);
End of FileEnd of File
●
There are a number of ways to test for the end-of-fileThere are a number of ways to test for the end-of-file
condition. Another way is to use the value returned bycondition. Another way is to use the value returned by
thethe fscanffscanf function:function:
FILE *fptr1;FILE *fptr1;
int istatus ;int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == feof(fptr1) )if ( istatus == feof(fptr1) )
{{
printf ("End-of-file encountered.n”) ;printf ("End-of-file encountered.n”) ;
}}
Reading and Writing FilesReading and Writing Files
#include <stdio.h>#include <stdio.h>
int main ( )int main ( )
{{
FILE *outfile, *infile ;FILE *outfile, *infile ;
int b = 5, f ;int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;float a = 13.72, c = 6.68, e, g ;
outfile = fopen ("testdata", "w") ;outfile = fopen ("testdata", "w") ;
fprintf (outfile, “ %f %d %f ", a, b, c) ;fprintf (outfile, “ %f %d %f ", a, b, c) ;
fclose (outfile) ;fclose (outfile) ;
infile = fopen ("testdata", "r") ;infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;fscanf (infile,"%f %d %f", &e, &f, &g) ;
printf (“ %f %d %f n ", a, b, c) ;printf (“ %f %d %f n ", a, b, c) ;
printf (“ %f %d %f n ", e, f, g) ;printf (“ %f %d %f n ", e, f, g) ;
}}
ExampleExample
#include <stdio.h>#include <stdio.h>
#include<conio.h>#include<conio.h>
int main()int main()
{{
char ch;char ch;
FILE *fp;FILE *fp;
fp=fopen("out.txt","r");fp=fopen("out.txt","r");
while(!feof(fp))while(!feof(fp))
{{
ch=getc(fp);ch=getc(fp);
printf("n%c",ch);printf("n%c",ch);
}}
fcloseall( );fcloseall( );
}}
freadfread ()()
Declaration:Declaration:
size_t fread(void *ptr, size_t size, size_t n, FILE *stream);size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
Remarks:Remarks:
fread reads a specified number of equal-sizedfread reads a specified number of equal-sized
data items from an input stream into a block.data items from an input stream into a block.
ptr = Points to a block into which data is readptr = Points to a block into which data is read
size = Length of each item read, in bytessize = Length of each item read, in bytes
n = Number of items readn = Number of items read
stream = file pointerstream = file pointer
ExampleExample
Example:Example:
#include <stdio.h>#include <stdio.h>
int main()int main()
{{
FILE *f;FILE *f;
char buffer[11];char buffer[11];
if (f = fopen("fred.txt", “r”))if (f = fopen("fred.txt", “r”))
{{
fread(buffer, 1, 10, f);fread(buffer, 1, 10, f);
buffer[10] = 0;buffer[10] = 0;
fclose(f);fclose(f);
printf("first 10 characters of the file:n%sn", buffer);printf("first 10 characters of the file:n%sn", buffer);
}}
return 0;return 0;
}}
fwrite()fwrite()
Declaration:Declaration:
size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);
Remarks:Remarks:
fwrite appends a specified number of equal-sized data items to anfwrite appends a specified number of equal-sized data items to an
output file.output file.
ptr = Pointer to any object; the data written begins at ptrptr = Pointer to any object; the data written begins at ptr
size = Length of each item of datasize = Length of each item of data
n =Number of data items to be appendedn =Number of data items to be appended
stream = file pointerstream = file pointer
ExampleExample
Example:Example:
#include <stdio.h>#include <stdio.h>
int main()int main()
{{
char a[10]={'1','2','3','4','5','6','7','8','9','a'};char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;FILE *fs;
fs=fopen("Project.txt","w");fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);fwrite(a,1,10,fs);
fclose(fs);fclose(fs);
return 0;return 0;
}}
fseek()fseek()
This function sets the file position indicator for the stream pointed to by streamThis function sets the file position indicator for the stream pointed to by stream
or you can say it seeks a specified place within a file and modify it.or you can say it seeks a specified place within a file and modify it.
SEEK_SETSEEK_SET Seeks from beginning of fileSeeks from beginning of file
SEEK_CURSEEK_CUR Seeks from current positionSeeks from current position
SEEK_ENDSEEK_END Seeks from end of fileSeeks from end of file
Example:Example:
#include#include <stdio.h><stdio.h>
intint mainmain()()
{{
FILE * f;FILE * f;
f = fopen("myfile.txt", "w");f = fopen("myfile.txt", "w");
fputs("Hello World", f);fputs("Hello World", f);
fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_ENDfseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END
fputs(" India", f);fputs(" India", f);
fclose(f);fclose(f);
returnreturn 0;0;
}}
ftell()ftell()
offset = ftell( file pointer );offset = ftell( file pointer );
"ftell" returns the current position for input or output on the file"ftell" returns the current position for input or output on the file
#include <stdio.h>#include <stdio.h>
int main(void)int main(void)
{{
FILE *stream;FILE *stream;
stream = fopen("MYFILE.TXT", "w");stream = fopen("MYFILE.TXT", "w");
fprintf(stream, "This is a test");fprintf(stream, "This is a test");
printf("The file pointer is at byte %ldn", ftell(stream));printf("The file pointer is at byte %ldn", ftell(stream));
fclose(stream);fclose(stream);
return 0;return 0;
}}
Thank you
This presentation is created using LibreOffice Impress 3.6.2.2

More Related Content

What's hot (20)

File handling in c
File handling in c File handling in c
File handling in c
 
File in C language
File in C languageFile in C language
File in C language
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File Management
File ManagementFile Management
File Management
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examples
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File handling in C
File handling in CFile handling in C
File handling in C
 
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
 
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
filefile
file
 
File Handling in C
File Handling in C File Handling in C
File Handling in C
 
Files in C
Files in CFiles in C
Files in C
 
File Management in C
File Management in CFile Management in C
File Management in C
 
file management in c language
file management in c languagefile management in c language
file management in c language
 

Viewers also liked

UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C ProgrammingKamal Acharya
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
Palindrome number program in c
Palindrome number program in cPalindrome number program in c
Palindrome number program in cmohdshanu
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
C programs
C programsC programs
C programsMinu S
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
File Management Presentation
File Management PresentationFile Management Presentation
File Management PresentationSgtMasterGunz
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSSAmit Tyagi
 

Viewers also liked (18)

UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
week-20x
week-20xweek-20x
week-20x
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
C programming language
C programming languageC programming language
C programming language
 
Palindrome number program in c
Palindrome number program in cPalindrome number program in c
Palindrome number program in c
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Structure c
Structure cStructure c
Structure c
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
C programs
C programsC programs
C programs
 
C Programming
C ProgrammingC Programming
C Programming
 
Structure in c
Structure in cStructure in c
Structure in c
 
file handling c++
file handling c++file handling c++
file handling c++
 
File Management Presentation
File Management PresentationFile Management Presentation
File Management Presentation
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 

Similar to Module 03 File Handling in C

Similar to Module 03 File Handling in C (20)

File management
File managementFile management
File management
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
File handling
File handlingFile handling
File handling
 
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
 
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
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
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 management
File managementFile management
File management
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Unit 8
Unit 8Unit 8
Unit 8
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
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
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
file handling1
file handling1file handling1
file handling1
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File management
File managementFile management
File management
 

More from Tushar B Kute

Apache Pig: A big data processor
Apache Pig: A big data processorApache Pig: A big data processor
Apache Pig: A big data processorTushar B Kute
 
01 Introduction to Android
01 Introduction to Android01 Introduction to Android
01 Introduction to AndroidTushar B Kute
 
Ubuntu OS and it's Flavours
Ubuntu OS and it's FlavoursUbuntu OS and it's Flavours
Ubuntu OS and it's FlavoursTushar B Kute
 
Install Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteInstall Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteTushar B Kute
 
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteInstall Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteTushar B Kute
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftpTushar B Kute
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
 
Implementation of FIFO in Linux
Implementation of FIFO in LinuxImplementation of FIFO in Linux
Implementation of FIFO in LinuxTushar B Kute
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in LinuxTushar B Kute
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsTushar B Kute
 
Part 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxPart 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxTushar B Kute
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxTushar B Kute
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Tushar B Kute
 
Open source applications softwares
Open source applications softwaresOpen source applications softwares
Open source applications softwaresTushar B Kute
 
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Tushar B Kute
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteTushar B Kute
 
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTechnical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTushar B Kute
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 

More from Tushar B Kute (20)

Apache Pig: A big data processor
Apache Pig: A big data processorApache Pig: A big data processor
Apache Pig: A big data processor
 
01 Introduction to Android
01 Introduction to Android01 Introduction to Android
01 Introduction to Android
 
Ubuntu OS and it's Flavours
Ubuntu OS and it's FlavoursUbuntu OS and it's Flavours
Ubuntu OS and it's Flavours
 
Install Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteInstall Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. Kute
 
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteInstall Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. Kute
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Implementation of FIFO in Linux
Implementation of FIFO in LinuxImplementation of FIFO in Linux
Implementation of FIFO in Linux
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Part 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxPart 04 Creating a System Call in Linux
Part 04 Creating a System Call in Linux
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
 
Open source applications softwares
Open source applications softwaresOpen source applications softwares
Open source applications softwares
 
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTechnical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrc
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 

Recently uploaded

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 

Recently uploaded (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 

Module 03 File Handling in C

  • 1. See through C Module 3 File Handling Tushar B Kute http://tusharkute.com
  • 2. What is a File?What is a File? ● AA filefile is a collection of related data that ais a collection of related data that a computers treats as a single unit.computers treats as a single unit. ● Computers store files to secondary storage soComputers store files to secondary storage so that the contents of files remain intact when athat the contents of files remain intact when a computer shuts down.computer shuts down. ● When a computer reads a file, it copies the fileWhen a computer reads a file, it copies the file from the storage device to memory; when itfrom the storage device to memory; when it writes to a file, it transfers data from memory towrites to a file, it transfers data from memory to the storage device.the storage device. ● C uses a structure calledC uses a structure called FILEFILE (defined in(defined in stdio.hstdio.h) to store the attributes of a file.) to store the attributes of a file.
  • 3. Steps in Processing a FileSteps in Processing a File 1.1. Create the stream via a pointer variable using theCreate the stream via a pointer variable using the FILEFILE structure:structure: FILE *p;FILE *p; 2.2. Open the file, associating the stream name with the fileOpen the file, associating the stream name with the file name.name. 3.3. Read or write the data.Read or write the data. 4.4. Close the file.Close the file.
  • 4. The basic file operations areThe basic file operations are ● fopen - open a file- specify how its openedfopen - open a file- specify how its opened (read/write) and type (binary/text)(read/write) and type (binary/text) ● fclose - close an opened filefclose - close an opened file ● fread - read from a filefread - read from a file ● fwrite - write to a filefwrite - write to a file ● fseek/fsetpos - move a file pointer tofseek/fsetpos - move a file pointer to somewhere in a file.somewhere in a file. ● ftell/fgetpos - tell you where the file pointer isftell/fgetpos - tell you where the file pointer is located.located.
  • 5. File Open ModesFile Open Modes
  • 6. More on File Open ModesMore on File Open Modes
  • 7. Additionally,Additionally, ● r+ - open for reading and writing, start at beginningr+ - open for reading and writing, start at beginning ● w+ - open for reading and writing (overwrite file)w+ - open for reading and writing (overwrite file) ● a+ - open for reading and writing (append if file exists)a+ - open for reading and writing (append if file exists)
  • 8. File OpenFile Open ● The file open function (The file open function (fopenfopen) serves two purposes:) serves two purposes: – It makes the connection between the physical file andIt makes the connection between the physical file and the stream.the stream. – It creates “a program file structure to store theIt creates “a program file structure to store the information” C needs to process the file.information” C needs to process the file. ● Syntax:Syntax: filepointer=filepointer=fopen(“filename”, “mode”);fopen(“filename”, “mode”);
  • 9. More OnMore On fopenfopen ● The file mode tells C how the program willThe file mode tells C how the program will use the file.use the file. ● The filename indicates the system nameThe filename indicates the system name and location for the file.and location for the file. ● We assign the return value ofWe assign the return value of fopenfopen to ourto our pointer variable:pointer variable: spData = fopen(“MYFILE.TXT”, “w”);spData = fopen(“MYFILE.TXT”, “w”); spData = fopen(“A:MYFILE.TXT”, “w”);spData = fopen(“A:MYFILE.TXT”, “w”);
  • 10. More OnMore On fopenfopen
  • 11. Closing a FileClosing a File ● When we finish with a mode, we need to close the fileWhen we finish with a mode, we need to close the file before ending the program or beginning another modebefore ending the program or beginning another mode with that same file.with that same file. ● To close a file, we useTo close a file, we use fclosefclose and the pointer variable:and the pointer variable: fclose(spData);fclose(spData);
  • 12. fprintf()fprintf() Syntax:Syntax: fprintf (fp,"string",variables);fprintf (fp,"string",variables); Example:Example: int i = 12;int i = 12; float x = 2.356;float x = 2.356; char ch = 's';char ch = 's'; FILE *fp;FILE *fp; fp=fopen(“out.txt”,”w”);fp=fopen(“out.txt”,”w”); fprintf (fp, "%d %f %c", i, x,fprintf (fp, "%d %f %c", i, x, ch);ch);
  • 13. fscanf()fscanf() Syntax:Syntax: fscanf (fp,"string",identifiers);fscanf (fp,"string",identifiers); Example:Example: FILE *fp;FILE *fp; fp=fopen(“input.txt”,”r”);fp=fopen(“input.txt”,”r”); int i;int i; fscanf (fp,“%d",&i);fscanf (fp,“%d",&i);
  • 14. fgetc()fgetc() Syntax:Syntax: identifier = fgetc (file pointer);identifier = fgetc (file pointer); Example:Example: FILE *fp;FILE *fp; fp=fopen(“input.txt”,”r”);fp=fopen(“input.txt”,”r”); char ch;char ch; ch = fgetc (fp);ch = fgetc (fp);
  • 15. fputc()fputc() write a single character to the output file, pointed to by fp.write a single character to the output file, pointed to by fp. Example:Example: FILE *fp;FILE *fp; char ch;char ch; fputc (ch,fp);fputc (ch,fp);
  • 16. End of FileEnd of File ● There are a number of ways to test for the end-of-fileThere are a number of ways to test for the end-of-file condition. Another way is to use the value returned bycondition. Another way is to use the value returned by thethe fscanffscanf function:function: FILE *fptr1;FILE *fptr1; int istatus ;int istatus ; istatus = fscanf (fptr1, "%d", &var) ;istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == feof(fptr1) )if ( istatus == feof(fptr1) ) {{ printf ("End-of-file encountered.n”) ;printf ("End-of-file encountered.n”) ; }}
  • 17. Reading and Writing FilesReading and Writing Files #include <stdio.h>#include <stdio.h> int main ( )int main ( ) {{ FILE *outfile, *infile ;FILE *outfile, *infile ; int b = 5, f ;int b = 5, f ; float a = 13.72, c = 6.68, e, g ;float a = 13.72, c = 6.68, e, g ; outfile = fopen ("testdata", "w") ;outfile = fopen ("testdata", "w") ; fprintf (outfile, “ %f %d %f ", a, b, c) ;fprintf (outfile, “ %f %d %f ", a, b, c) ; fclose (outfile) ;fclose (outfile) ; infile = fopen ("testdata", "r") ;infile = fopen ("testdata", "r") ; fscanf (infile,"%f %d %f", &e, &f, &g) ;fscanf (infile,"%f %d %f", &e, &f, &g) ; printf (“ %f %d %f n ", a, b, c) ;printf (“ %f %d %f n ", a, b, c) ; printf (“ %f %d %f n ", e, f, g) ;printf (“ %f %d %f n ", e, f, g) ; }}
  • 18. ExampleExample #include <stdio.h>#include <stdio.h> #include<conio.h>#include<conio.h> int main()int main() {{ char ch;char ch; FILE *fp;FILE *fp; fp=fopen("out.txt","r");fp=fopen("out.txt","r"); while(!feof(fp))while(!feof(fp)) {{ ch=getc(fp);ch=getc(fp); printf("n%c",ch);printf("n%c",ch); }} fcloseall( );fcloseall( ); }}
  • 19. freadfread ()() Declaration:Declaration: size_t fread(void *ptr, size_t size, size_t n, FILE *stream);size_t fread(void *ptr, size_t size, size_t n, FILE *stream); Remarks:Remarks: fread reads a specified number of equal-sizedfread reads a specified number of equal-sized data items from an input stream into a block.data items from an input stream into a block. ptr = Points to a block into which data is readptr = Points to a block into which data is read size = Length of each item read, in bytessize = Length of each item read, in bytes n = Number of items readn = Number of items read stream = file pointerstream = file pointer
  • 20. ExampleExample Example:Example: #include <stdio.h>#include <stdio.h> int main()int main() {{ FILE *f;FILE *f; char buffer[11];char buffer[11]; if (f = fopen("fred.txt", “r”))if (f = fopen("fred.txt", “r”)) {{ fread(buffer, 1, 10, f);fread(buffer, 1, 10, f); buffer[10] = 0;buffer[10] = 0; fclose(f);fclose(f); printf("first 10 characters of the file:n%sn", buffer);printf("first 10 characters of the file:n%sn", buffer); }} return 0;return 0; }}
  • 21. fwrite()fwrite() Declaration:Declaration: size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream); Remarks:Remarks: fwrite appends a specified number of equal-sized data items to anfwrite appends a specified number of equal-sized data items to an output file.output file. ptr = Pointer to any object; the data written begins at ptrptr = Pointer to any object; the data written begins at ptr size = Length of each item of datasize = Length of each item of data n =Number of data items to be appendedn =Number of data items to be appended stream = file pointerstream = file pointer
  • 22. ExampleExample Example:Example: #include <stdio.h>#include <stdio.h> int main()int main() {{ char a[10]={'1','2','3','4','5','6','7','8','9','a'};char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs;FILE *fs; fs=fopen("Project.txt","w");fs=fopen("Project.txt","w"); fwrite(a,1,10,fs);fwrite(a,1,10,fs); fclose(fs);fclose(fs); return 0;return 0; }}
  • 23. fseek()fseek() This function sets the file position indicator for the stream pointed to by streamThis function sets the file position indicator for the stream pointed to by stream or you can say it seeks a specified place within a file and modify it.or you can say it seeks a specified place within a file and modify it. SEEK_SETSEEK_SET Seeks from beginning of fileSeeks from beginning of file SEEK_CURSEEK_CUR Seeks from current positionSeeks from current position SEEK_ENDSEEK_END Seeks from end of fileSeeks from end of file Example:Example: #include#include <stdio.h><stdio.h> intint mainmain()() {{ FILE * f;FILE * f; f = fopen("myfile.txt", "w");f = fopen("myfile.txt", "w"); fputs("Hello World", f);fputs("Hello World", f); fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_ENDfseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END fputs(" India", f);fputs(" India", f); fclose(f);fclose(f); returnreturn 0;0; }}
  • 24. ftell()ftell() offset = ftell( file pointer );offset = ftell( file pointer ); "ftell" returns the current position for input or output on the file"ftell" returns the current position for input or output on the file #include <stdio.h>#include <stdio.h> int main(void)int main(void) {{ FILE *stream;FILE *stream; stream = fopen("MYFILE.TXT", "w");stream = fopen("MYFILE.TXT", "w"); fprintf(stream, "This is a test");fprintf(stream, "This is a test"); printf("The file pointer is at byte %ldn", ftell(stream));printf("The file pointer is at byte %ldn", ftell(stream)); fclose(stream);fclose(stream); return 0;return 0; }}
  • 25. Thank you This presentation is created using LibreOffice Impress 3.6.2.2