SlideShare a Scribd company logo
1 of 13
Lecture 22Lecture 22
Version 1.0Version 1.0
More on FilesMore on Files
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
A File Copy ProgramA File Copy Program
 We will take a look at how you read from a fileWe will take a look at how you read from a file
 And then how you can put the content of thatAnd then how you can put the content of that
file into another filefile into another file
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Read (r)Read (r)
Searches the fileSearches the file
If the search is successfulIf the search is successful
load the file into memoryload the file into memory
set a pointer at the beginning of the fileset a pointer at the beginning of the file
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Only reads from fileOnly reads from file
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Write (w)Write (w)
Searches the fileSearches the file
If the file existsIf the file exists
it is overwrittenit is overwritten
If notIf not
a new file is createda new file is created
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Only writes to fileOnly writes to file
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Append (a)Append (a)
Searches the fileSearches the file
If the search is successfulIf the search is successful
load the file into memoryload the file into memory
set a pointer at the end of the fileset a pointer at the end of the file
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Adding new contents in the fileAdding new contents in the file
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Append Plus (a+)Append Plus (a+)
Searches the fileSearches the file
If the search is successfulIf the search is successful
load the file into memoryload the file into memory
If not foundIf not found
create a new filecreate a new file
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Reading, appending in fileReading, appending in file
Cannot modifyCannot modify
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Read Plus (r+)Read Plus (r+)
Searches the fileSearches the file
If the search is successfulIf the search is successful
load the file into memoryload the file into memory
set a pointer at first characterset a pointer at first character
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Reading, writing, modifying contents in the fileReading, writing, modifying contents in the file
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
File Opening ModesFile Opening Modes
 Write Plus (w+)Write Plus (w+)
Searches the fileSearches the file
If the file existsIf the file exists
it is overwrittenit is overwritten
If notIf not
a new file is createda new file is created
If the file cannot be openedIf the file cannot be opened
returns NULLreturns NULL
Writing, reading and modifying contentsWriting, reading and modifying contents
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
String in FilesString in Files
 So far we have seen how to manipulateSo far we have seen how to manipulate
characters in filescharacters in files
 Now, we will see that handling strings in files isNow, we will see that handling strings in files is
as easy as handling charactersas easy as handling characters
 At first we will see how to write strings into aAt first we will see how to write strings into a
filefile
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
 Now we will see how to read strings from filesNow we will see how to read strings from files
13Rushdi Shams, Dept of CSE, KUET, Bangladesh

More Related Content

Similar to Lec 22. Files (Part II)

Similar to Lec 22. Files (Part II) (20)

File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
File accessing modes in c
File accessing modes in cFile accessing modes in c
File accessing modes in c
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File handling
File handlingFile handling
File handling
 
File handing in C
File handing in CFile handing in C
File handing in C
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
what are python basics.pptx.Join Python training in Chandigarh
what are python basics.pptx.Join Python training in Chandigarhwhat are python basics.pptx.Join Python training in Chandigarh
what are python basics.pptx.Join Python training in Chandigarh
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
Com ed 5 final
Com ed 5 finalCom ed 5 final
Com ed 5 final
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 

More from Rushdi Shams

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
Rushdi Shams
 

More from Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Lec 22. Files (Part II)

  • 1. Lecture 22Lecture 22 Version 1.0Version 1.0 More on FilesMore on Files
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh A File Copy ProgramA File Copy Program  We will take a look at how you read from a fileWe will take a look at how you read from a file  And then how you can put the content of thatAnd then how you can put the content of that file into another filefile into another file
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Read (r)Read (r) Searches the fileSearches the file If the search is successfulIf the search is successful load the file into memoryload the file into memory set a pointer at the beginning of the fileset a pointer at the beginning of the file If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Only reads from fileOnly reads from file
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Write (w)Write (w) Searches the fileSearches the file If the file existsIf the file exists it is overwrittenit is overwritten If notIf not a new file is createda new file is created If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Only writes to fileOnly writes to file
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Append (a)Append (a) Searches the fileSearches the file If the search is successfulIf the search is successful load the file into memoryload the file into memory set a pointer at the end of the fileset a pointer at the end of the file If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Adding new contents in the fileAdding new contents in the file
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Append Plus (a+)Append Plus (a+) Searches the fileSearches the file If the search is successfulIf the search is successful load the file into memoryload the file into memory If not foundIf not found create a new filecreate a new file If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Reading, appending in fileReading, appending in file Cannot modifyCannot modify
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Read Plus (r+)Read Plus (r+) Searches the fileSearches the file If the search is successfulIf the search is successful load the file into memoryload the file into memory set a pointer at first characterset a pointer at first character If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Reading, writing, modifying contents in the fileReading, writing, modifying contents in the file
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh File Opening ModesFile Opening Modes  Write Plus (w+)Write Plus (w+) Searches the fileSearches the file If the file existsIf the file exists it is overwrittenit is overwritten If notIf not a new file is createda new file is created If the file cannot be openedIf the file cannot be opened returns NULLreturns NULL Writing, reading and modifying contentsWriting, reading and modifying contents
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh String in FilesString in Files  So far we have seen how to manipulateSo far we have seen how to manipulate characters in filescharacters in files  Now, we will see that handling strings in files isNow, we will see that handling strings in files is as easy as handling charactersas easy as handling characters  At first we will see how to write strings into aAt first we will see how to write strings into a filefile
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh  Now we will see how to read strings from filesNow we will see how to read strings from files
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh