SlideShare a Scribd company logo
1 of 14
FILE MANAGEMENT in C
VIDHYA B
ASSISTANT PROFESSOR
Department of Computer Science with Data Analytics
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
1
FILES
2
Sri Ramakrishna College of Arts and Science
File -
Container for
Storing
Information
INTRODUCTION TO FILE
MANAGEMENT
• Console oriented – use
terminal (keyboard/screen)
• scanf(“%d”,&i) – read data
from keyboard
• printf(“%d”,i) – print data to
monitor
• Suitable for small volumes of
data
• Data lost when program
terminated
• Large data volumes
• E.g. physical experiments
(CERN collider), human
genome, population
records etc.
• Need for flexible
approach to store/retrieve
data
• Concept of files
3
Sri Ramakrishna College of Arts and Science
DEFINITION OF FILE
- A file is an object on a computer, stores data, information,
settings, or commands used with a computer program
- Two kinds of files in a system…
(i) Text files (ASCII) – ASCII codes of digits,
alphabets and symbols
(ii) Binary - contains collection of bytes (0’s and 1’s) and
are compiled version of text files.
4
Sri Ramakrishna College of Arts and Science
FILE OPERATIONS
 Creating a file
 Opening a file
 Closing a file
 Reading a file
 Writing in a file
5
Sri Ramakrishna College of Arts and Science
DEFINING AND OPENING FILE
To store data file in secondary memory (disc)
must specify to OS
 Filename (e.g. sort.c, input.data)
 Data structure (e.g. FILE)
 Purpose (e.g. reading, writing, appending)
6
Sri Ramakrishna College of Arts and Science
DEFINING AND OPENING FILE
String of characters that make up a valid filename for
OS
May contain two parts
 Primary
 Optional period with extension
Examples: a.out, prog.c, temp, text.out
7
Sri Ramakrishna College of Arts and Science
DEFINING A FILE
To have a way to access the files , let the program
keep track of the file being accessed
FILE - initialize an object of the type FILE
*fp - declare the pointer in order to point to the
file, to read from or to write on
fopen ("filename","mode") – file to open, with
file name and mode of opening as argument
8
Sri Ramakrishna College of Arts and Science
FILE *fp;
fp = fopen ("filename","mode");
OPENING A FILE
FILE *fp;
fp=fopen("home.txt", "r");
- The above statement will open the file
“home.txt” in read mode
9
Sri Ramakrishna College of Arts and Science
CLOSING A FILE
File must be closed as soon as all operations on it
completed
Ensures
All outstanding information associated with file flushed
out from buffers
All links to file broken
Accidental misuse of file prevented
If want to change mode of file, then first close and
open again
10
Sri Ramakrishna College of Arts and Science
CLOSING A FILE
Syntax: fclose(file_pointer);
Example:
FILE *p1, *p2;
p1 = fopen(“INPUT.txt”, “r”);
p2 =fopen(“OUTPUT.txt”, “w”);
……..
……..
fclose(p1);
fclose(p2);
pointer can be reused after closing
11
Sri Ramakrishna College of Arts and Science
MODES OF FILE OPERATION
12
Sri Ramakrishna College of Arts and Science
Mode Description
r Opens an existing text file for reading purpose
w Opens a text file for writing. If it does not exist,
then a new file is created
a Opens a text file for writing in appending mode.
If it does not exist, then a new file is created
MODES OF FILE OPERATION
13
Sri Ramakrishna College of Arts and Science
Mode Description
r+ Opens a text file for both reading and writing
w+ Opens a text file for both reading and writing. It
first truncates the file to zero length if it exists,
otherwise creates a file if it does not exist.
a+ Opens a text file for both reading and writing. It
creates the file if it does not exist. The reading
will start from the beginning but writing can only
be appended.
REFERENCES
14
Sri Ramakrishna College of Arts and Science
Programming in ANSI C
by
E. Balagurusamy.

More Related Content

Similar to unit 5-files.pptx

file handling1
file handling1file handling1
file handling1
student
 

Similar to unit 5-files.pptx (20)

Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
File management
File managementFile management
File management
 
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
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Handout#01
Handout#01Handout#01
Handout#01
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File management
File managementFile management
File management
 
File handling in C
File handling in CFile handling in C
File handling in C
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
file handling1
file handling1file handling1
file handling1
 
File in C language
File in C languageFile in C language
File in C language
 
Unit5
Unit5Unit5
Unit5
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
File management in C++
File management in C++File management in C++
File management in C++
 
MODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxMODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptx
 
File Handling
File HandlingFile Handling
File Handling
 

Recently uploaded

The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptxHVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 

unit 5-files.pptx

  • 1. FILE MANAGEMENT in C VIDHYA B ASSISTANT PROFESSOR Department of Computer Science with Data Analytics Sri Ramakrishna College of Arts and Science Coimbatore - 641 006 Tamil Nadu, India 1
  • 2. FILES 2 Sri Ramakrishna College of Arts and Science File - Container for Storing Information
  • 3. INTRODUCTION TO FILE MANAGEMENT • Console oriented – use terminal (keyboard/screen) • scanf(“%d”,&i) – read data from keyboard • printf(“%d”,i) – print data to monitor • Suitable for small volumes of data • Data lost when program terminated • Large data volumes • E.g. physical experiments (CERN collider), human genome, population records etc. • Need for flexible approach to store/retrieve data • Concept of files 3 Sri Ramakrishna College of Arts and Science
  • 4. DEFINITION OF FILE - A file is an object on a computer, stores data, information, settings, or commands used with a computer program - Two kinds of files in a system… (i) Text files (ASCII) – ASCII codes of digits, alphabets and symbols (ii) Binary - contains collection of bytes (0’s and 1’s) and are compiled version of text files. 4 Sri Ramakrishna College of Arts and Science
  • 5. FILE OPERATIONS  Creating a file  Opening a file  Closing a file  Reading a file  Writing in a file 5 Sri Ramakrishna College of Arts and Science
  • 6. DEFINING AND OPENING FILE To store data file in secondary memory (disc) must specify to OS  Filename (e.g. sort.c, input.data)  Data structure (e.g. FILE)  Purpose (e.g. reading, writing, appending) 6 Sri Ramakrishna College of Arts and Science
  • 7. DEFINING AND OPENING FILE String of characters that make up a valid filename for OS May contain two parts  Primary  Optional period with extension Examples: a.out, prog.c, temp, text.out 7 Sri Ramakrishna College of Arts and Science
  • 8. DEFINING A FILE To have a way to access the files , let the program keep track of the file being accessed FILE - initialize an object of the type FILE *fp - declare the pointer in order to point to the file, to read from or to write on fopen ("filename","mode") – file to open, with file name and mode of opening as argument 8 Sri Ramakrishna College of Arts and Science FILE *fp; fp = fopen ("filename","mode");
  • 9. OPENING A FILE FILE *fp; fp=fopen("home.txt", "r"); - The above statement will open the file “home.txt” in read mode 9 Sri Ramakrishna College of Arts and Science
  • 10. CLOSING A FILE File must be closed as soon as all operations on it completed Ensures All outstanding information associated with file flushed out from buffers All links to file broken Accidental misuse of file prevented If want to change mode of file, then first close and open again 10 Sri Ramakrishna College of Arts and Science
  • 11. CLOSING A FILE Syntax: fclose(file_pointer); Example: FILE *p1, *p2; p1 = fopen(“INPUT.txt”, “r”); p2 =fopen(“OUTPUT.txt”, “w”); …….. …….. fclose(p1); fclose(p2); pointer can be reused after closing 11 Sri Ramakrishna College of Arts and Science
  • 12. MODES OF FILE OPERATION 12 Sri Ramakrishna College of Arts and Science Mode Description r Opens an existing text file for reading purpose w Opens a text file for writing. If it does not exist, then a new file is created a Opens a text file for writing in appending mode. If it does not exist, then a new file is created
  • 13. MODES OF FILE OPERATION 13 Sri Ramakrishna College of Arts and Science Mode Description r+ Opens a text file for both reading and writing w+ Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. a+ Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
  • 14. REFERENCES 14 Sri Ramakrishna College of Arts and Science Programming in ANSI C by E. Balagurusamy.