SlideShare une entreprise Scribd logo
1  sur  13
Computer Programming & Utilization
1
 The functions that we use such as scanf( ) & printf( ) are there
to read and write data. These I/O functions always use the
terminal (Keyboard and Screen) as the target place.
 These functions work where the data is small, but the entire
process becomes cumbersome and time consuming when it
comes to many real-life problems which involve large volumes
of data through terminals & due to this the entire data is lost
when either a program is terminated or computer is turned off.
 So a flexible approach was brought in, leading to the concept
of files in which data can be stored on the disks and read
whenever necessary, without destroying the data.
2
 A file is a place on the disk where a group of related data is
stored.
 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.
 Two distinct ways to perform file operations in C:-
low-level I/O – Uses UNIX system calls.
high-level I/O – Uses functions in C’s standard I/O library.
3
4
Function Name Operation
fopen() Creates a new file/Opens an existing file for use.
fclose() Closes a file which has been opened for use.
getc() Reads a character from a file.
putc() Writes a character to a file.
fprintf() Writes a set of data values to a file.
fscanf() Reads a set of data values to a file.
getw() Reads an integer from file.
putw() Writes an integer from file.
fseek() Sets the position to a desired a point in the file.
ftell() Gives the correct position in the file.
rewind() Sets the position to the beginning of the file.
 Filename is a string of characters that make up a valid filename
for the operating system.
 Data structure of a file is defined as FILE in the library of
standard I/O functions definitions.
 C uses a structure called FILE (defined in stdio.h) to store the
attributes of a file. FILE is a defined data type.
 General format for defining & opening a file:-
5
FILE *fp;
fp = fopen(“filename” , “mode”);
 The first statement declares the variable fp as a “pointer to the
data type FILE”.
 The second statement opens the file filename and assigns an
identifier to the FILE type pointer fp. It also specifies the
purpose of opening this file. The mode does this job.
 Modes:-
r – Open the file for reading only.
w – Open the file for writing only.
a – Open the file for appending data to it.
6
FILE *p1, *p2;
p1 = fopen(“CPU” , “r”);
p2 = fopen(“Data” , “w”);
 Closing a file ensures that all outstanding information
associated with the file is flushed out from the buffers and all
links to the file are broken.
 Syntax for closing a file:-
 Example:-
7
fclose(file_pointer);
FILE *p1, *p2;
p1 = fopen(“cpu” , “r”);
p2 = fopen(“data” , “w”);
……………..
fclose(p1);
fclose(p2);
 getc and putc are the simplest file I/O functions. These are
analogous to getchar and putchar functions and handle one
character at a time.
 getc is used to read a character from a file that has been
opened, while putc is used to write a character to the file that
has been opened.
 Syntax for getc:-
 Syntax for putc:-
8
identifier = getc(file pointer);
putc(identifier , file pointer);
 Example:-
9
FILE *fp1, *fp2;
fp1= fopen(“input.txt” , “r”);
fp2 = fopen(“output.txt” , “w”);
char ch;
ch = getc (fp1);
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
 The getw and putw are integer-oriented functions. They are
similar to getc and putc functions and are used to read and
write integer values.
 Syntax for getw:-
 Syntax for putw:-
10
getw(file pointer);
putw(integer , file pointer);
 Most compilers support functions fprintf and fscanf ,that can
handle a group of mixed data simultaneously.
 The functions fprintf and fscanf are identical to the familiar
printf and scanf functions, except of course that they work on
files.
 Syntax & Example of fprintf:-
11
fprintf(fp ,“Control String”, list);
fprintf(fp,“%s %d %f”,name,age,8);
 Syntax & Example of fscanf:-
12
fscanf(fp ,“Control String”, list);
fprintf(fp,“%s %d”,name,&quantity);
13

Contenu connexe

Tendances (20)

Break and continue
Break and continueBreak and continue
Break and continue
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Strings in C
Strings in CStrings in C
Strings in C
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Strings
StringsStrings
Strings
 
Structure in C
Structure in CStructure in C
Structure in C
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
structure and union
structure and unionstructure and union
structure and union
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 

Similaire à File Management in C

Similaire à File Management in C (20)

File management
File managementFile management
File management
 
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
 
Unit5
Unit5Unit5
Unit5
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
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 Organization
File OrganizationFile Organization
File Organization
 
Files in C
Files in CFiles in C
Files in C
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
File in C language
File in C languageFile in C language
File in C language
 
File management
File managementFile management
File management
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
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_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
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 

Plus de Paurav Shah

Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithmsPaurav Shah
 
Cost and Cost Concepts
Cost and Cost ConceptsCost and Cost Concepts
Cost and Cost ConceptsPaurav Shah
 
3 D (3-Dimensional) Glasses
3 D (3-Dimensional) Glasses3 D (3-Dimensional) Glasses
3 D (3-Dimensional) GlassesPaurav Shah
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data StructurePaurav Shah
 
Design issues with constraints of E-R model
Design issues with constraints of E-R modelDesign issues with constraints of E-R model
Design issues with constraints of E-R modelPaurav Shah
 
Android and iOS Mobile OS
Android and iOS Mobile OSAndroid and iOS Mobile OS
Android and iOS Mobile OSPaurav Shah
 
Decoders-Digital Electronics
Decoders-Digital ElectronicsDecoders-Digital Electronics
Decoders-Digital ElectronicsPaurav Shah
 
Contributor Career Strategies
Contributor Career StrategiesContributor Career Strategies
Contributor Career StrategiesPaurav Shah
 
Semiconductor and It's Importance
Semiconductor and It's ImportanceSemiconductor and It's Importance
Semiconductor and It's ImportancePaurav Shah
 
Builiding A Good Personality
Builiding A Good PersonalityBuiliding A Good Personality
Builiding A Good PersonalityPaurav Shah
 

Plus de Paurav Shah (11)

Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Cost and Cost Concepts
Cost and Cost ConceptsCost and Cost Concepts
Cost and Cost Concepts
 
3 D (3-Dimensional) Glasses
3 D (3-Dimensional) Glasses3 D (3-Dimensional) Glasses
3 D (3-Dimensional) Glasses
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
 
Design issues with constraints of E-R model
Design issues with constraints of E-R modelDesign issues with constraints of E-R model
Design issues with constraints of E-R model
 
Android and iOS Mobile OS
Android and iOS Mobile OSAndroid and iOS Mobile OS
Android and iOS Mobile OS
 
Decoders-Digital Electronics
Decoders-Digital ElectronicsDecoders-Digital Electronics
Decoders-Digital Electronics
 
Contributor Career Strategies
Contributor Career StrategiesContributor Career Strategies
Contributor Career Strategies
 
Semiconductor and It's Importance
Semiconductor and It's ImportanceSemiconductor and It's Importance
Semiconductor and It's Importance
 
Earthing
EarthingEarthing
Earthing
 
Builiding A Good Personality
Builiding A Good PersonalityBuiliding A Good Personality
Builiding A Good Personality
 

Dernier

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Dernier (20)

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

File Management in C

  • 1. Computer Programming & Utilization 1
  • 2.  The functions that we use such as scanf( ) & printf( ) are there to read and write data. These I/O functions always use the terminal (Keyboard and Screen) as the target place.  These functions work where the data is small, but the entire process becomes cumbersome and time consuming when it comes to many real-life problems which involve large volumes of data through terminals & due to this the entire data is lost when either a program is terminated or computer is turned off.  So a flexible approach was brought in, leading to the concept of files in which data can be stored on the disks and read whenever necessary, without destroying the data. 2
  • 3.  A file is a place on the disk where a group of related data is stored.  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.  Two distinct ways to perform file operations in C:- low-level I/O – Uses UNIX system calls. high-level I/O – Uses functions in C’s standard I/O library. 3
  • 4. 4 Function Name Operation fopen() Creates a new file/Opens an existing file for use. fclose() Closes a file which has been opened for use. getc() Reads a character from a file. putc() Writes a character to a file. fprintf() Writes a set of data values to a file. fscanf() Reads a set of data values to a file. getw() Reads an integer from file. putw() Writes an integer from file. fseek() Sets the position to a desired a point in the file. ftell() Gives the correct position in the file. rewind() Sets the position to the beginning of the file.
  • 5.  Filename is a string of characters that make up a valid filename for the operating system.  Data structure of a file is defined as FILE in the library of standard I/O functions definitions.  C uses a structure called FILE (defined in stdio.h) to store the attributes of a file. FILE is a defined data type.  General format for defining & opening a file:- 5 FILE *fp; fp = fopen(“filename” , “mode”);
  • 6.  The first statement declares the variable fp as a “pointer to the data type FILE”.  The second statement opens the file filename and assigns an identifier to the FILE type pointer fp. It also specifies the purpose of opening this file. The mode does this job.  Modes:- r – Open the file for reading only. w – Open the file for writing only. a – Open the file for appending data to it. 6 FILE *p1, *p2; p1 = fopen(“CPU” , “r”); p2 = fopen(“Data” , “w”);
  • 7.  Closing a file ensures that all outstanding information associated with the file is flushed out from the buffers and all links to the file are broken.  Syntax for closing a file:-  Example:- 7 fclose(file_pointer); FILE *p1, *p2; p1 = fopen(“cpu” , “r”); p2 = fopen(“data” , “w”); …………….. fclose(p1); fclose(p2);
  • 8.  getc and putc are the simplest file I/O functions. These are analogous to getchar and putchar functions and handle one character at a time.  getc is used to read a character from a file that has been opened, while putc is used to write a character to the file that has been opened.  Syntax for getc:-  Syntax for putc:- 8 identifier = getc(file pointer); putc(identifier , file pointer);
  • 9.  Example:- 9 FILE *fp1, *fp2; fp1= fopen(“input.txt” , “r”); fp2 = fopen(“output.txt” , “w”); char ch; ch = getc (fp1); putc(ch,fp2); fclose(fp1); fclose(fp2);
  • 10.  The getw and putw are integer-oriented functions. They are similar to getc and putc functions and are used to read and write integer values.  Syntax for getw:-  Syntax for putw:- 10 getw(file pointer); putw(integer , file pointer);
  • 11.  Most compilers support functions fprintf and fscanf ,that can handle a group of mixed data simultaneously.  The functions fprintf and fscanf are identical to the familiar printf and scanf functions, except of course that they work on files.  Syntax & Example of fprintf:- 11 fprintf(fp ,“Control String”, list); fprintf(fp,“%s %d %f”,name,age,8);
  • 12.  Syntax & Example of fscanf:- 12 fscanf(fp ,“Control String”, list); fprintf(fp,“%s %d”,name,&quantity);
  • 13. 13