SlideShare une entreprise Scribd logo
1  sur  25
Assignment on -“File Handling in C” 
1
Contents : 
Acknowledgement ------------------- 
File Handling in C ---------------------- 
What is a File? ------------------------ 
Steps in Processing a File ------------ 
The basic file operations are -------- 
File Open Modes------------------------ 
More on File Open Modes------------ 
Additionally, ----------------------------- 
File Open --------------------------------- 
More On fopen ------------------------- 
More On fopen ------------------------- 
Closing a File ----------------------------- 
fprintf() ------------------------ 
fscanf() ------------------------ 
getc() ------------------------ 
putc() ------------------------ 
End of File ------------------------ 
Reading and Writing Files ------------- 
Example ------------------------ ------ 
fread () ------------------------ ------ - 
Example ------------------------ ----- 
fwrite() ------------------------ ------ -- 
Example ------------------------ ------ 
fseek() ------------------------ ------ -- 
ftell() ------------------------------ ------ 
Conclusion ------------------------ ----- 
2
What is a File? 
• A file is a collection of related data that a 
computers treats as a single unit. 
• Computers store files to secondary storage so 
that the contents of files remain intact when a 
computer shuts down. 
• When a computer reads a file, it copies the file 
from the storage device to memory; when it 
writes to a file, it transfers data from memory to 
the storage device. 
• C uses a structure called FILE (defined in stdio.h) 
to store the attributes of a file. 
3
Steps in Processing a 
File 
1. Create the stream via a pointer variable using 
the FFIILLEE structure: 
FFIILLEE **pp;; 
2. Open the file, associating the stream name 
with the file name. 
3. Read or write the data. 
4. Close the file. 
4
The basic file operations are 
• fopen - open a file- specify how its opened 
(read/write) and type (binary/text) 
• fclose - close an opened file 
• fread - read from a file 
• fwrite - write to a file 
• fseek/fsetpos - move a file pointer to somewhere in 
a file. 
• ftell/fgetpos - tell you where the file pointer is 
located. 
5
File Open Modes 
from Table 7-1 in Forouzan & Gilberg, p. 400 
6
More on File Open Modes 
from Figure 7-4 in Forouzan & Gilberg, p. 401 
7
Additionally, 
• r+ - open for reading and writing, start at 
beginning 
• w+ - open for reading and writing (overwrite 
file) 
• a+ - open for reading and writing (append if 
file exists) 
8
File Open 
• The file open function (ffooppeenn) serves two 
purposes: 
– It makes the connection between the physical file 
and the stream. 
– It creates “a program file structure to store the 
information” C needs to process the file. 
• Syntax: 
ffiilleeppooiinntteerr==ffooppeenn((““ffiilleennaammee””,, ““mmooddee””));; 
9
More On : fopen 
• The file mode tells C how the program will use 
the file. 
• The filename indicates the system name and 
location for the file. 
• We assign the return value of ffooppeenn to our 
pointer variable: 
ssppDDaattaa == ffooppeenn((““MMYYFFIILLEE..TTXXTT””,, ““ww””));; 
ssppDDaattaa == ffooppeenn((““AA::MMYYFFIILLEE..TTXXTT””,, ““ww””));; 
10
More On : fopen 
from Figure 7-3 in Forouzan & Gilberg, p. 399 
11
Closing a File 
• When we finish with a mode, we need to 
close the file before ending the program or 
beginning another mode with that same file. 
• To close a file, we use ffcclloossee and the 
pointer variable: 
ffcclloossee((ssppDDaattaa));; 
12
fprintf() 
SSyynnttaaxx:: 
fprintf (fp,"string",variables); 
EExxaammppllee:: 
int i = 12; 
float x = 2.356; 
char ch = 's'; 
FILE *fp; 
fp=fopen(“out.txt”,”w”); 
fprintf (fp, "%d %f %c", i, x, ch); 
13
fscanf() 
SSyynnttaaxx:: 
fscanf (fp,"string",identifiers); 
EExxaammppllee:: 
FILE *fp; 
Fp=fopen(“input.txt”,”r”); 
int i; 
fscanf (fp,“%d",i); 
14
getc() 
SSyynnttaaxx:: 
identifier = getc (file pointer); 
EExxaammppllee:: 
FILE *fp; 
fp=fopen(“input.txt”,”r”); 
char ch; 
ch = getc (fp); 
15
putc() 
write a single character to the output file, 
pointed to by fp. 
EExxaammppllee:: 
FILE *fp; 
char ch; 
putc (ch,fp); 
16
End of File {eof} 
• There are a number of ways to test for the end-of-file 
condition. Another way is to use the value returned by the 
ffssccaannff function: 
FILE *fptr1; 
int istatus ; 
istatus = fscanf (fptr1, "%d", &var) ; 
if ( istatus == feof(fptr1) ) 
{ 
printf ("End-of-file encountered.n”) ; 
} 
17
Reading and Writing Files 
#include <stdio.h> 
int main ( ) 
{ 
FILE *outfile, *infile ; 
int b = 5, f ; 
float a = 13.72, c = 6.68, e, g ; 
outfile = fopen ("testdata", "w") ; 
fprintf (outfile, “ %f %d %f ", a, b, c) ; 
fclose (outfile) ; 
infile = fopen ("testdata", "r") ; 
fscanf (infile,"%f %d %f", &e, &f, &g) ; 
printf (“ %f %d %f n ", a, b, c) ; 
printf (“ %f %d %f n ", e, f, g) ; 
} 
18
Example 
#include <stdio.h> 
#include<conio.h> 
void main() 
{ 
char ch; 
FILE *fp; 
fp=fopen("out.txt","r"); 
while(!feof(fp)) 
{ 
ch=getc(fp); 
printf("n%c",ch); 
} 
getch(); 
} 
19
fread () 
Declaration: 
size_t fread(void *ptr, size_tt ssiizzee,, ssiizzee__tt nn,, FFIILLEE **ssttrreeaamm));; 
Remarks: 
fread reads a specified number of equal-sized 
data items from an input stream into a block. 
ptr = Points to a block into which data is read 
size = Length of each item read, in bytes 
n = Number of items read 
stream = file pointer 
20
Example 
EExxaammppllee:: 
#include <stdio.h> 
int main() 
{ 
FILE *f; 
char buffer[11]; 
if (f = fopen("fred.txt", “r”)) 
{ 
fread(buffer, 1, 10, f); 
buffer[10] = 0; 
fclose(f); 
printf("first 10 characters of the file:n%sn", buffer); 
} 
return 0; 
} 
21
fwrite() 
Declaration: 
size_t fwrite(const void *ptr, size_t size, ssiizzee__tt nn,, FFIILLEE**ssttrreeaamm));; 
Remarks: 
fwrite appends a specified number of equal-sized data items to an output file. 
ppttrr == PPooiinntteerr ttoo aannyy oobbjjeecctt;; tthhee ddaattaa wwrriitttteenn bbeeggiinnss aatt ppttrr 
ssiizzee == LLeennggtthh ooff eeaacchh iitteemm ooff ddaattaa 
nn ==NNuummbbeerr ooff ddaattaa iitteemmss ttoo bbee aappppeennddeedd 
ssttrreeaamm == ffiillee ppooiinntteerr 
22
Example 
EExxaammppllee:: 
#include <stdio.h> 
int main() 
{ 
char a[10]={'1','2','3','4','5','6','7','8','9','a'}; 
FILE *fs; 
fs=fopen("Project.txt","w"); 
fwrite(a,1,10,fs); 
fclose(fs); 
return 0; 
} 
23
fseek() 
This 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. 
SSEEEEKK__SSEETT SSeeeekkss ffrroomm bbeeggiinnnniinngg ooff ffiillee 
SSEEEEKK__CCUURR SSeeeekkss ffrroomm ccuurrrreenntt ppoossiittiioonn 
SSEEEEKK__EENNDD SSeeeekkss ffrroomm eenndd ooff ffiillee 
EExxaammppllee:: 
#include <stdio.h> 
int main() 
{ 
FILE * f; 
f = fopen("myfile.txt", "w"); 
fputs("Hello World", f); 
fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END 
fputs(" India", f); 
fclose(f); 
return 0; 
} 
24
ftell() 
offset = ftell( ffiillee ppooiinntteerr ));; 
"ftell" returns the current position for input or output on the file 
#include <stdio.h> 
int main(void) 
{ 
FILE *stream; 
stream = fopen("MYFILE.TXT", "w"); 
fprintf(stream, "This is a test"); 
printf("The file pointer is at byte %ldn", ftell(stream)); 
fclose(stream); 
return 0; 
} 
25

Contenu connexe

Tendances

file system in operating system
file system in operating systemfile system in operating system
file system in operating system
tittuajay
 
Xml web services
Xml web servicesXml web services
Xml web services
Raghu nath
 

Tendances (20)

File handling in C
File handling in CFile handling in C
File handling in C
 
File management
File managementFile management
File management
 
Vi editor in linux
Vi editor in linuxVi editor in linux
Vi editor in linux
 
File Management
File ManagementFile Management
File Management
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
file system in operating system
file system in operating systemfile system in operating system
file system in operating system
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
Xml web services
Xml web servicesXml web services
Xml web services
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Cloud storage advantages vs disadvantages
Cloud storage advantages vs disadvantagesCloud storage advantages vs disadvantages
Cloud storage advantages vs disadvantages
 
File system security
File system securityFile system security
File system security
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Python file handling
Python file handlingPython file handling
Python file handling
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 

Similaire à File handling in c

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
 

Similaire à File handling in c (20)

Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Unit5
Unit5Unit5
Unit5
 
File management
File managementFile management
File management
 
File handling
File handlingFile handling
File handling
 
1file handling
1file handling1file handling
1file handling
 
File management
File managementFile management
File management
 
Unit 8
Unit 8Unit 8
Unit 8
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
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
File handling in cFile handling in c
File handling in c
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Unit v
Unit vUnit v
Unit v
 
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
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
Files in c
Files in cFiles in c
Files in c
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
 
C-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptxC-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptx
 
File Management in C
File Management in CFile Management in C
File Management in C
 

Plus de Vikash Dhal (7)

Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
 
Packet switching
Packet switchingPacket switching
Packet switching
 
Raid
RaidRaid
Raid
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 

Dernier

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Dernier (20)

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 

File handling in c

  • 1. Assignment on -“File Handling in C” 1
  • 2. Contents : Acknowledgement ------------------- File Handling in C ---------------------- What is a File? ------------------------ Steps in Processing a File ------------ The basic file operations are -------- File Open Modes------------------------ More on File Open Modes------------ Additionally, ----------------------------- File Open --------------------------------- More On fopen ------------------------- More On fopen ------------------------- Closing a File ----------------------------- fprintf() ------------------------ fscanf() ------------------------ getc() ------------------------ putc() ------------------------ End of File ------------------------ Reading and Writing Files ------------- Example ------------------------ ------ fread () ------------------------ ------ - Example ------------------------ ----- fwrite() ------------------------ ------ -- Example ------------------------ ------ fseek() ------------------------ ------ -- ftell() ------------------------------ ------ Conclusion ------------------------ ----- 2
  • 3. What is a File? • A file is a collection of related data that a computers treats as a single unit. • Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down. • When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device. • C uses a structure called FILE (defined in stdio.h) to store the attributes of a file. 3
  • 4. Steps in Processing a File 1. Create the stream via a pointer variable using the FFIILLEE structure: FFIILLEE **pp;; 2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file. 4
  • 5. The basic file operations are • fopen - open a file- specify how its opened (read/write) and type (binary/text) • fclose - close an opened file • fread - read from a file • fwrite - write to a file • fseek/fsetpos - move a file pointer to somewhere in a file. • ftell/fgetpos - tell you where the file pointer is located. 5
  • 6. File Open Modes from Table 7-1 in Forouzan & Gilberg, p. 400 6
  • 7. More on File Open Modes from Figure 7-4 in Forouzan & Gilberg, p. 401 7
  • 8. Additionally, • r+ - open for reading and writing, start at beginning • w+ - open for reading and writing (overwrite file) • a+ - open for reading and writing (append if file exists) 8
  • 9. File Open • The file open function (ffooppeenn) serves two purposes: – It makes the connection between the physical file and the stream. – It creates “a program file structure to store the information” C needs to process the file. • Syntax: ffiilleeppooiinntteerr==ffooppeenn((““ffiilleennaammee””,, ““mmooddee””));; 9
  • 10. More On : fopen • The file mode tells C how the program will use the file. • The filename indicates the system name and location for the file. • We assign the return value of ffooppeenn to our pointer variable: ssppDDaattaa == ffooppeenn((““MMYYFFIILLEE..TTXXTT””,, ““ww””));; ssppDDaattaa == ffooppeenn((““AA::MMYYFFIILLEE..TTXXTT””,, ““ww””));; 10
  • 11. More On : fopen from Figure 7-3 in Forouzan & Gilberg, p. 399 11
  • 12. Closing a File • When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file. • To close a file, we use ffcclloossee and the pointer variable: ffcclloossee((ssppDDaattaa));; 12
  • 13. fprintf() SSyynnttaaxx:: fprintf (fp,"string",variables); EExxaammppllee:: int i = 12; float x = 2.356; char ch = 's'; FILE *fp; fp=fopen(“out.txt”,”w”); fprintf (fp, "%d %f %c", i, x, ch); 13
  • 14. fscanf() SSyynnttaaxx:: fscanf (fp,"string",identifiers); EExxaammppllee:: FILE *fp; Fp=fopen(“input.txt”,”r”); int i; fscanf (fp,“%d",i); 14
  • 15. getc() SSyynnttaaxx:: identifier = getc (file pointer); EExxaammppllee:: FILE *fp; fp=fopen(“input.txt”,”r”); char ch; ch = getc (fp); 15
  • 16. putc() write a single character to the output file, pointed to by fp. EExxaammppllee:: FILE *fp; char ch; putc (ch,fp); 16
  • 17. End of File {eof} • There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the ffssccaannff function: FILE *fptr1; int istatus ; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == feof(fptr1) ) { printf ("End-of-file encountered.n”) ; } 17
  • 18. Reading and Writing Files #include <stdio.h> int main ( ) { FILE *outfile, *infile ; int b = 5, f ; float a = 13.72, c = 6.68, e, g ; outfile = fopen ("testdata", "w") ; fprintf (outfile, “ %f %d %f ", a, b, c) ; fclose (outfile) ; infile = fopen ("testdata", "r") ; fscanf (infile,"%f %d %f", &e, &f, &g) ; printf (“ %f %d %f n ", a, b, c) ; printf (“ %f %d %f n ", e, f, g) ; } 18
  • 19. Example #include <stdio.h> #include<conio.h> void main() { char ch; FILE *fp; fp=fopen("out.txt","r"); while(!feof(fp)) { ch=getc(fp); printf("n%c",ch); } getch(); } 19
  • 20. fread () Declaration: size_t fread(void *ptr, size_tt ssiizzee,, ssiizzee__tt nn,, FFIILLEE **ssttrreeaamm));; Remarks: fread reads a specified number of equal-sized data items from an input stream into a block. ptr = Points to a block into which data is read size = Length of each item read, in bytes n = Number of items read stream = file pointer 20
  • 21. Example EExxaammppllee:: #include <stdio.h> int main() { FILE *f; char buffer[11]; if (f = fopen("fred.txt", “r”)) { fread(buffer, 1, 10, f); buffer[10] = 0; fclose(f); printf("first 10 characters of the file:n%sn", buffer); } return 0; } 21
  • 22. fwrite() Declaration: size_t fwrite(const void *ptr, size_t size, ssiizzee__tt nn,, FFIILLEE**ssttrreeaamm));; Remarks: fwrite appends a specified number of equal-sized data items to an output file. ppttrr == PPooiinntteerr ttoo aannyy oobbjjeecctt;; tthhee ddaattaa wwrriitttteenn bbeeggiinnss aatt ppttrr ssiizzee == LLeennggtthh ooff eeaacchh iitteemm ooff ddaattaa nn ==NNuummbbeerr ooff ddaattaa iitteemmss ttoo bbee aappppeennddeedd ssttrreeaamm == ffiillee ppooiinntteerr 22
  • 23. Example EExxaammppllee:: #include <stdio.h> int main() { char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs); return 0; } 23
  • 24. fseek() This 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. SSEEEEKK__SSEETT SSeeeekkss ffrroomm bbeeggiinnnniinngg ooff ffiillee SSEEEEKK__CCUURR SSeeeekkss ffrroomm ccuurrrreenntt ppoossiittiioonn SSEEEEKK__EENNDD SSeeeekkss ffrroomm eenndd ooff ffiillee EExxaammppllee:: #include <stdio.h> int main() { FILE * f; f = fopen("myfile.txt", "w"); fputs("Hello World", f); fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END fputs(" India", f); fclose(f); return 0; } 24
  • 25. ftell() offset = ftell( ffiillee ppooiinntteerr ));; "ftell" returns the current position for input or output on the file #include <stdio.h> int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w"); fprintf(stream, "This is a test"); printf("The file pointer is at byte %ldn", ftell(stream)); fclose(stream); return 0; } 25