SlideShare a Scribd company logo
1 of 18
File
Accessing
Modes in C
MOTAPALUKULA MANOJ
20951A0585
Basics of File Handling in C
So far the operations using C program are done on a prompt /
terminal which is not stored anywhere.
But in the software industry, most of the programs are written
to store the information fetched from the program.
One such way is to store the fetched information in a file.
Different operations that can be performed on a file are:
1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgets)
4. Writing to a file (fprintf or fputs)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
The text in the brackets denotes the functions used for performing those operations.
Functions in File Operations:
Opening or creating file
For opening a file, fopen function is used with the required access
modes. Some of the commonly used file access modes are
mentioned below.
File opening modes in C:
•“r” – Searches file. If the file is opened successfully fopen( ) loads it
into memory and sets up a pointer which points to the first character in
it. If the file cannot be opened fopen( ) returns NULL.
•“rb” – Open for reading in binary mode. If the file does not exist,
fopen( ) returns NULL.
“w” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t
exist, a new file is created. Returns NULL, if unable to open file.
“wb” – Open for writing in binary mode. If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
“a” – Searches file. If the file is opened successfully fopen( ) loads it into memory
and sets up a pointer that points to the last character in it. If the file doesn’t exist, a
new file is created. Returns NULL, if unable to open file.
“ab” – Open for append in binary mode. Data is added to the end of the file. If the
file does not exist, it will be created.
NEXT
“w” – Searches file. If the file exists, its contents are overwritten. If the file
doesn’t exist, a new file is created. Returns NULL, if unable to open file.
“wb” – Open for writing in binary mode. If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
“a” – Searches file. If the file is opened successfully fopen( ) loads it into
memory and sets up a pointer that points to the last character in it. If the
file doesn’t exist, a new file is created. Returns NULL, if unable to open
file.
NEXT
“ab” – Open for append in binary mode. Data is added to the end of the file.
If the file does not exist, it will be created.
“r+” – Searches file. If is opened successfully fopen( ) loads it into memory
and sets up a pointer which points to the first character in it. Returns NULL, if
unable to open the file.
“rb+” – Open for both reading and writing in binary mode. If the file does not
exist, fopen( ) returns NULL.
“w+” – Searches file. If the file exists, its contents are overwritten. If the file
doesn’t exist a new file is created. Returns NULL, if unable to open file.
NEXT
“wb+” – Open for both reading and writing in binary mode. If the
file exists, its contents are overwritten. If the file does not exist, it
will be created.
“a+” – Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer which points to the last
character in it. If the file doesn’t exist, a new file is created.
Returns NULL, if unable to open file.
“ab+” – Open for both reading and appending in binary mode. If
the file does not exist, it will be created.
NEXT
As given above, if you want to perform operations on a
binary file, then you have to append ‘b’ at the last. For
example, instead of “w”, you have to use “wb”, instead of
“a+” you have to use “a+b”. For performing the
operations on the file, a special pointer called File pointer
is used which is declared as
NEXT
the second parameter can be changed to contain all the
attributes listed in the above table.
FILE *filePointer;
So, the file can be opened as
filePointer = fopen(“fileName.txt”, “w”)
Reading from a file –
The file read operations can be performed using functions
fscanf or fgets. Both the functions performed the same
operations as that of scanf and gets but with an additional
parameter, the file pointer. So, it depends on you if you
want to read the file line by line or character by character.
And the code snippet for reading a file is as:
FILE * filePointer;
filePointer = fopen(“fileName.txt”, “r”);
fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
Writing a file –:
The file write operations can be perfomed by the functions fprintf and fputs
with
similarities to read operations. The snippet for writing to a file is as :
FILE *filePointer ;
filePointer = fopen(“fileName.txt”, “w”);
fprintf(filePointer, "%s %s %s %d", "We", "are", "in",
2012);
Closing a file –:
After every successful fie operations, you must always
close a file.
For closing a file, you have to use fclose function.
The snippet for closing a file is given as :
FILE *filePointer ;
filePointer= fopen(“fileName.txt”, “w”);
---------- Some file Operations -------
Program to Open a File, Write in it, And Close the File
// C program to Open a File,
// Write in it, And Close the File
# include <stdio.h>
# include <string.h>
int main( )
{
// Declare the file pointer
FILE *filePointer ;
// Get the data to be written in file
char dataToBeWritten[50]= "GOOGLE-A search engine";
// Open the existing file GfgTest.c using fopen()
// in write mode using "w" attribute
filePointer = fopen("GfgTest.c", "w") ;
NEXT
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "GfgTest.c file failed to open." ) ;
}
else
{
printf("The file is now opened.n") ;
// Write the dataToBeWritten into the file
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("n", filePointer) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file GfgTest.cn");
printf("The file is now closed.") ;
}
return 0;
}
Thankyou
- BY
- MOTAPALUKULA MANOJ
- 20951A0585

More Related Content

What's hot

File Management in C
File Management in CFile Management in C
File Management in CPaurav Shah
 
Presentation on 8086 Microprocessor
Presentation  on   8086 MicroprocessorPresentation  on   8086 Microprocessor
Presentation on 8086 MicroprocessorNahian Ahmed
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Computer Memory Hierarchy Computer Architecture
Computer Memory Hierarchy Computer ArchitectureComputer Memory Hierarchy Computer Architecture
Computer Memory Hierarchy Computer ArchitectureHaris456
 
Processor Organization and Architecture
Processor Organization and ArchitectureProcessor Organization and Architecture
Processor Organization and ArchitectureVinit Raut
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptxMemonaMemon1
 
programming techniques
programming techniquesprogramming techniques
programming techniquesRamaPrabha24
 
Task communication
Task communicationTask communication
Task communication1jayanti
 
Neural Networks and Genetic Algorithms Multiobjective acceleration
Neural Networks and Genetic Algorithms Multiobjective accelerationNeural Networks and Genetic Algorithms Multiobjective acceleration
Neural Networks and Genetic Algorithms Multiobjective accelerationArmando Vieira
 

What's hot (20)

File Management in C
File Management in CFile Management in C
File Management in C
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Presentation on 8086 Microprocessor
Presentation  on   8086 MicroprocessorPresentation  on   8086 Microprocessor
Presentation on 8086 Microprocessor
 
Semiconductor memory
Semiconductor memorySemiconductor memory
Semiconductor memory
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Call by value
Call by valueCall by value
Call by value
 
functions of C++
functions of C++functions of C++
functions of C++
 
8086 micro processor
8086 micro processor8086 micro processor
8086 micro processor
 
Strings in C
Strings in CStrings in C
Strings in C
 
Computer Memory Hierarchy Computer Architecture
Computer Memory Hierarchy Computer ArchitectureComputer Memory Hierarchy Computer Architecture
Computer Memory Hierarchy Computer Architecture
 
Processor Organization and Architecture
Processor Organization and ArchitectureProcessor Organization and Architecture
Processor Organization and Architecture
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
 
Function in c
Function in cFunction in c
Function in c
 
programming techniques
programming techniquesprogramming techniques
programming techniques
 
Task communication
Task communicationTask communication
Task communication
 
Neural Networks and Genetic Algorithms Multiobjective acceleration
Neural Networks and Genetic Algorithms Multiobjective accelerationNeural Networks and Genetic Algorithms Multiobjective acceleration
Neural Networks and Genetic Algorithms Multiobjective acceleration
 
Lecture 13
Lecture 13Lecture 13
Lecture 13
 

Similar to File Access Modes C

Similar to File Access Modes C (20)

Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptx
 
File handing in C
File handing in CFile handing in C
File handing in C
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
File handling C program
File handling C programFile handling C program
File handling C program
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
File management
File managementFile management
File management
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
Programming in C
Programming in CProgramming in C
Programming in C
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 

Recently uploaded

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
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
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
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
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
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
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
 
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
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

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
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
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
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
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
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
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...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

File Access Modes C

  • 2. Basics of File Handling in C So far the operations using C program are done on a prompt / terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file.
  • 3. Different operations that can be performed on a file are: 1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”) 2. Opening an existing file (fopen) 3. Reading from file (fscanf or fgets) 4. Writing to a file (fprintf or fputs) 5. Moving to a specific location in a file (fseek, rewind) 6. Closing a file (fclose) The text in the brackets denotes the functions used for performing those operations.
  • 4. Functions in File Operations:
  • 5. Opening or creating file For opening a file, fopen function is used with the required access modes. Some of the commonly used file access modes are mentioned below. File opening modes in C: •“r” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen( ) returns NULL. •“rb” – Open for reading in binary mode. If the file does not exist, fopen( ) returns NULL.
  • 6. “w” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. “wb” – Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created. “a” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. “ab” – Open for append in binary mode. Data is added to the end of the file. If the file does not exist, it will be created. NEXT
  • 7. “w” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. “wb” – Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created. “a” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. NEXT
  • 8. “ab” – Open for append in binary mode. Data is added to the end of the file. If the file does not exist, it will be created. “r+” – Searches file. If is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file. “rb+” – Open for both reading and writing in binary mode. If the file does not exist, fopen( ) returns NULL. “w+” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open file. NEXT
  • 9. “wb+” – Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created. “a+” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. “ab+” – Open for both reading and appending in binary mode. If the file does not exist, it will be created. NEXT
  • 10. As given above, if you want to perform operations on a binary file, then you have to append ‘b’ at the last. For example, instead of “w”, you have to use “wb”, instead of “a+” you have to use “a+b”. For performing the operations on the file, a special pointer called File pointer is used which is declared as NEXT
  • 11. the second parameter can be changed to contain all the attributes listed in the above table. FILE *filePointer; So, the file can be opened as filePointer = fopen(“fileName.txt”, “w”)
  • 12. Reading from a file – The file read operations can be performed using functions fscanf or fgets. Both the functions performed the same operations as that of scanf and gets but with an additional parameter, the file pointer. So, it depends on you if you want to read the file line by line or character by character. And the code snippet for reading a file is as: FILE * filePointer; filePointer = fopen(“fileName.txt”, “r”); fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
  • 13. Writing a file –: The file write operations can be perfomed by the functions fprintf and fputs with similarities to read operations. The snippet for writing to a file is as : FILE *filePointer ; filePointer = fopen(“fileName.txt”, “w”); fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);
  • 14. Closing a file –: After every successful fie operations, you must always close a file. For closing a file, you have to use fclose function. The snippet for closing a file is given as : FILE *filePointer ; filePointer= fopen(“fileName.txt”, “w”); ---------- Some file Operations -------
  • 15. Program to Open a File, Write in it, And Close the File // C program to Open a File, // Write in it, And Close the File # include <stdio.h> # include <string.h> int main( ) { // Declare the file pointer FILE *filePointer ; // Get the data to be written in file char dataToBeWritten[50]= "GOOGLE-A search engine"; // Open the existing file GfgTest.c using fopen() // in write mode using "w" attribute filePointer = fopen("GfgTest.c", "w") ;
  • 16. NEXT // Check if this filePointer is null // which maybe if the file does not exist if ( filePointer == NULL ) { printf( "GfgTest.c file failed to open." ) ; } else { printf("The file is now opened.n") ; // Write the dataToBeWritten into the file if ( strlen ( dataToBeWritten ) > 0 ) { // writing in the file using fputs() fputs(dataToBeWritten, filePointer) ; fputs("n", filePointer) ; }
  • 17. // Closing the file using fclose() fclose(filePointer) ; printf("Data successfully written in file GfgTest.cn"); printf("The file is now closed.") ; } return 0; }
  • 18. Thankyou - BY - MOTAPALUKULA MANOJ - 20951A0585