SlideShare a Scribd company logo
Presentation Group 3th
Chapter 8
File management in C
2
Profesor: Edwin o. omandi
Console oriented Input/Output
• 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
Real-life applications
• Large data volumes
• Example: physical experiments (CERN collider), human
genome, population records etc.
• Need for flexible approach to store/retrieve data
• Concept of files
Files
• File – place on disc where group of related data is stored
– E.g. your C programs, executables
• High-level programming languages support file operations
– Naming
– Opening
– Reading
– Writing
– Closing
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)
Filename
• 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
General format for opening file
• fp
– contains all information about file
– Communication link between system and program
• Mode can be
– r open file for reading only
– w open file for writing only
– a open file for appending (adding) data
FILE *fp; /*variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”);
/*opens file with name filename , assigns identifier to fp */
Different modes
• Writing mode
– if file already exists then contents are deleted,
– else new file with specified name created
• Appending mode
– if file already exists then file opened with contents safe
– else new file created
• Reading mode
– if file already exists then opened with contents safe
– else error occurs.
FILE *p1, *p2;
p1 = fopen(“data”,”r”);
p2= fopen(“results”, w”);
Additional modes
• r+ open to beginning for both reading/writing
• w+ same as w except both for reading and writing
• a+ same as ‘a’ except both for reading and writing
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
Closing a file
• pointer can be reused after closing
Syntax: fclose(file_pointer);
Example:
FILE *p1, *p2;
p1 = fopen(“INPUT.txt”, “r”);
p2 =fopen(“OUTPUT.txt”, “w”);
……..
……..
fclose(p1);
fclose(p2);
Input/Output operations on files
• C provides several different functions for reading/writing
• getc() – read a character
• putc() – write a character
• fprintf() – write set of data values
• fscanf() – read set of data values
• getw() – read integer
• putw() – write integer
Errors that occur during I/O
• Typical errors that occur
– trying to read beyond end-of-file
– trying to use a file that has not been opened
– perform operation on file not permitted by ‘fopen’ mode
– open file with invalid filename
– write to write-protected file
THANK YOU


More Related Content

PPTX
File Handling in C Programming for Beginners
PPTX
C-Programming File-handling-C.pptx
PPTX
C-Programming File-handling-C.pptx
PPT
File Management
PPT
C-Programming Chapter 5 File-handling-C.ppt
PPTX
file management in c language
PPT
File handling-dutt
PPSX
1file handling
File Handling in C Programming for Beginners
C-Programming File-handling-C.pptx
C-Programming File-handling-C.pptx
File Management
C-Programming Chapter 5 File-handling-C.ppt
file management in c language
File handling-dutt
1file handling

Similar to finally.c.ppt (20)

PPT
C-Programming Chapter 5 File-handling-C.ppt
PPTX
File Handling.pptx
PPTX
unit 5-files.pptx
PPTX
Engineering Computers L34-L35-File Handling.pptx
PPT
How to do file-handling - in C language
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
PDF
Unit 5 File handling in C programming.pdf
PPTX
File management
PPTX
File_Handling_in_C_Programming for First Year Engineering
PPTX
File handling in C
PPTX
03-01-File Handling python.pptx
PPT
Files_in_C.ppt
PPTX
File Handling Python
PPTX
File handing in C
PPTX
PPS PPT 2.pptx
PPTX
Files in Python.pptx
PPTX
Files in Python.pptx
PPTX
File handling With Solve Programs
PPT
Filing system in PHP
PDF
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
C-Programming Chapter 5 File-handling-C.ppt
File Handling.pptx
unit 5-files.pptx
Engineering Computers L34-L35-File Handling.pptx
How to do file-handling - in C language
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
Unit 5 File handling in C programming.pdf
File management
File_Handling_in_C_Programming for First Year Engineering
File handling in C
03-01-File Handling python.pptx
Files_in_C.ppt
File Handling Python
File handing in C
PPS PPT 2.pptx
Files in Python.pptx
Files in Python.pptx
File handling With Solve Programs
Filing system in PHP
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf

Recently uploaded (20)

PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Welding lecture in detail for understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Sustainable Sites - Green Building Construction
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
web development for engineering and engineering
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
Geodesy 1.pptx...............................................
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Welding lecture in detail for understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Sustainable Sites - Green Building Construction
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
OOP with Java - Java Introduction (Basics)
web development for engineering and engineering
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx

finally.c.ppt

  • 2. Chapter 8 File management in C 2 Profesor: Edwin o. omandi
  • 3. Console oriented Input/Output • 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
  • 4. Real-life applications • Large data volumes • Example: physical experiments (CERN collider), human genome, population records etc. • Need for flexible approach to store/retrieve data • Concept of files
  • 5. Files • File – place on disc where group of related data is stored – E.g. your C programs, executables • High-level programming languages support file operations – Naming – Opening – Reading – Writing – Closing
  • 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)
  • 7. Filename • 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
  • 8. General format for opening file • fp – contains all information about file – Communication link between system and program • Mode can be – r open file for reading only – w open file for writing only – a open file for appending (adding) data FILE *fp; /*variable fp is pointer to type FILE*/ fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns identifier to fp */
  • 9. Different modes • Writing mode – if file already exists then contents are deleted, – else new file with specified name created • Appending mode – if file already exists then file opened with contents safe – else new file created • Reading mode – if file already exists then opened with contents safe – else error occurs. FILE *p1, *p2; p1 = fopen(“data”,”r”); p2= fopen(“results”, w”);
  • 10. Additional modes • r+ open to beginning for both reading/writing • w+ same as w except both for reading and writing • a+ same as ‘a’ except both for reading and writing
  • 11. 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
  • 12. Closing a file • pointer can be reused after closing Syntax: fclose(file_pointer); Example: FILE *p1, *p2; p1 = fopen(“INPUT.txt”, “r”); p2 =fopen(“OUTPUT.txt”, “w”); …….. …….. fclose(p1); fclose(p2);
  • 13. Input/Output operations on files • C provides several different functions for reading/writing • getc() – read a character • putc() – write a character • fprintf() – write set of data values • fscanf() – read set of data values • getw() – read integer • putw() – write integer
  • 14. Errors that occur during I/O • Typical errors that occur – trying to read beyond end-of-file – trying to use a file that has not been opened – perform operation on file not permitted by ‘fopen’ mode – open file with invalid filename – write to write-protected file