SlideShare a Scribd company logo
1 of 25
File Handling
Advanced Higher Programming
What is a file?
 Up until now, any stored data within a
program is lost when the program
closes.
 A file is a permanent way to store data
File Handling
Three types of file can be used for
storing data
 SequentialSequential
 RandomRandom
 BinaryBinary
Sequential Files
Sequential files are useful for:
 Storing text
 Easy implementation in programs
 Where real-time editing of file(s) is not
required
Random Files
Random file structures are useful for
 Files that require real-time editing
 Storing records
Binary Files
Binary Files are useful for
 Storing numbers, programs and images
 Where no defined file structure is
present
 They will not be used in this course
Sequential Files
 Have a universal standard format and
are used in text editors such as
windows notepad
 Numerical data is stored as a string
e.g., 5.32 would be stored as “5.32”
 They are read from start to finish and so
cannot be read and written to
simultaneously
Sequential Files
 Data is ALWAYS written and retrieved
as CHARACTERS.
 Hence, any number written in this
mode will result in the ASCII Value of
the
number being stored.
 For Example, The Number 17 is stored
as two separate characters "1" and
"7".
Which means that 17 is stored as [ 49
55 ] and not as [ 17 ].
Sequential Files
 Are like a one dimensional array
 The text, ONE DAT might be stored as:
“ D TENO CR”A EO
F
Sequential Files
Files are manipulated in 3 stages:
 File Open
 Process File
 Close File
Sequential Files
File OpenFile Open
 If the file does not exist it is createdcreated
and then openedand then opened by the operating
system.
 A portion of memory (RAM)memory (RAM) is
reserved by the Operating System.
Sequential Files
Processing a FileProcessing a File
 When a file is open it can be written towritten to
or reador read from. (both in the case of
random and binary files)
 Writing to a file will save it to backingbacking
store.store.
Sequential FilesSequential Files
Closing a fileClosing a file
 When a file has been opened and
processed it must then be closedmust then be closed.
 The Operating system will then
release the memory.release the memory.
Visual Basic
 VB supports all three file types, but you
are only likely to use two of them
 Text files
 Random Access files
Text Files
Sequential/Text Files
Input File opened for read-only access.
Output File opened for output which is only write-to
or create
Append The file is opened for adding new data to an
existing file. This is the default setting.
Random The file is open for random access. This is
writing or reading one record at a time.
Binary The file is opened in binary mode
Using the OpenFileDialog control
Dim Filename as String
OpenFileDialog1.ShowDialog()
Filename= OpenFileDialog1.Filename
lblFilename.Text = Filename
Opening FilesOpening Files
FileOpen(1, Filename, OpenMode.Input) ‘to read from the file
FileOpen(1, Filename, OpenMode.Output) ‘to write to the file
FileOpen(1, Filename, OpenMode.Append) ‘to write to the end of
the file
Note – 1 assigns the file the number 1. All files are
identified by a number, not by their name. If you have two
or more files open at once they must have different
numbers.
Opening Files
 The FileOpen statement opens a file if it
exists. When you open a file to read
from it, an error results if it does not
exists. When you open a file to write to
it, if it doesn’t exist FileOpen first
creates it and opens it.
 Filename contains the name and path
of the file
Opening (creating) a Sequential File
This algorithm would achieve this:
1.1. Enter FilenameEnter Filename
2.2. Open File for writingOpen File for writing
3.3. Input InformationInput Information
4.4. Save to fileSave to file
5.5. Close fileClose file
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
Filename= OpenFileDialog1.FileName
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
FileOpen(1, Filename, OpenMode.Output)
Writeline(1, DataToBeWritten)
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
FileClose (1)
Opening a sequential fileOpening a sequential file
The final code would look like:
Dim Filename as string
‘File manipulation Program
‘Create File
Private sub cmdCreateFile_Click()
OpenFileDialog1.ShowDialog()
Filename = FileDialog1.FileName
FileOpen(1,Filename, OpenMode.Output)
WriteLine(1,DataToBeWritten)
FileClose (1)
End Sub

More Related Content

What's hot

Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
sotlsoc
 

What's hot (20)

File Handling
File HandlingFile Handling
File Handling
 
python file handling
python file handlingpython file handling
python file handling
 
C files
C filesC files
C files
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
File Handling
File HandlingFile Handling
File Handling
 
Data file operations in C++ Base
Data file operations in C++ BaseData file operations in C++ Base
Data file operations in C++ Base
 
File handling
File handlingFile handling
File handling
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
H file handling
H file handlingH file handling
H file handling
 
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary fileCBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
 
File management
File managementFile management
File management
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
Handling computer files
Handling computer filesHandling computer files
Handling computer files
 
File Types in Data Structure
File Types in Data StructureFile Types in Data Structure
File Types in Data Structure
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
File operations
File operationsFile operations
File operations
 

Viewers also liked (15)

Standard Algorithms
Standard AlgorithmsStandard Algorithms
Standard Algorithms
 
Programming Paradigm
Programming ParadigmProgramming Paradigm
Programming Paradigm
 
Project management
Project managementProject management
Project management
 
Design Techniques
Design TechniquesDesign Techniques
Design Techniques
 
Database Systems and SQL
Database Systems and SQLDatabase Systems and SQL
Database Systems and SQL
 
Database and Web Integration
Database and Web IntegrationDatabase and Web Integration
Database and Web Integration
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Web Development
Web DevelopmentWeb Development
Web Development
 
Audio Compression
Audio CompressionAudio Compression
Audio Compression
 
Menu vb
Menu vbMenu vb
Menu vb
 
Web Pages
Web PagesWeb Pages
Web Pages
 
ISDD Media Types - File Compression
ISDD Media Types - File CompressionISDD Media Types - File Compression
ISDD Media Types - File Compression
 
ISDD Standard File Formats
ISDD Standard File FormatsISDD Standard File Formats
ISDD Standard File Formats
 
File Handling 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 C++
File Handling In C++
 

Similar to File handling

INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
AssadLeo1
 

Similar to File handling (20)

Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptxPOWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
 
Data file handling
Data file handlingData file handling
Data file handling
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 

More from SabahtHussein

More from SabahtHussein (20)

S3 HTML Hyperlinks and Images
S3 HTML Hyperlinks and ImagesS3 HTML Hyperlinks and Images
S3 HTML Hyperlinks and Images
 
S3 HTML Lists and Tables
S3 HTML Lists and TablesS3 HTML Lists and Tables
S3 HTML Lists and Tables
 
S3 HTML Structure and Formatting
S3 HTML Structure and FormattingS3 HTML Structure and Formatting
S3 HTML Structure and Formatting
 
S3 HTML Introduction
S3 HTML IntroductionS3 HTML Introduction
S3 HTML Introduction
 
S1 Programming Lesson 1
S1 Programming Lesson 1S1 Programming Lesson 1
S1 Programming Lesson 1
 
S1 Web Development
S1 Web DevelopmentS1 Web Development
S1 Web Development
 
Video Games Development
Video Games DevelopmentVideo Games Development
Video Games Development
 
S2 Data Types
S2 Data TypesS2 Data Types
S2 Data Types
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 
S3 SQL
S3 SQLS3 SQL
S3 SQL
 
Query Design
Query DesignQuery Design
Query Design
 
SQL PT
SQL PTSQL PT
SQL PT
 
Query design
Query designQuery design
Query design
 
S3 Field Types
S3 Field TypesS3 Field Types
S3 Field Types
 
Testing a Database
Testing a DatabaseTesting a Database
Testing a Database
 
Testing a Database
Testing a DatabaseTesting a Database
Testing a Database
 
Database Testing and Evaluation
Database Testing and EvaluationDatabase Testing and Evaluation
Database Testing and Evaluation
 
DPA and GDPR
DPA and GDPRDPA and GDPR
DPA and GDPR
 
Database Analysis
Database AnalysisDatabase Analysis
Database Analysis
 
Database Analysis
Database AnalysisDatabase Analysis
Database Analysis
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

File handling

  • 2. What is a file?  Up until now, any stored data within a program is lost when the program closes.  A file is a permanent way to store data
  • 3. File Handling Three types of file can be used for storing data  SequentialSequential  RandomRandom  BinaryBinary
  • 4. Sequential Files Sequential files are useful for:  Storing text  Easy implementation in programs  Where real-time editing of file(s) is not required
  • 5. Random Files Random file structures are useful for  Files that require real-time editing  Storing records
  • 6. Binary Files Binary Files are useful for  Storing numbers, programs and images  Where no defined file structure is present  They will not be used in this course
  • 7. Sequential Files  Have a universal standard format and are used in text editors such as windows notepad  Numerical data is stored as a string e.g., 5.32 would be stored as “5.32”  They are read from start to finish and so cannot be read and written to simultaneously
  • 8. Sequential Files  Data is ALWAYS written and retrieved as CHARACTERS.  Hence, any number written in this mode will result in the ASCII Value of the number being stored.  For Example, The Number 17 is stored as two separate characters "1" and "7". Which means that 17 is stored as [ 49 55 ] and not as [ 17 ].
  • 9. Sequential Files  Are like a one dimensional array  The text, ONE DAT might be stored as: “ D TENO CR”A EO F
  • 10. Sequential Files Files are manipulated in 3 stages:  File Open  Process File  Close File
  • 11. Sequential Files File OpenFile Open  If the file does not exist it is createdcreated and then openedand then opened by the operating system.  A portion of memory (RAM)memory (RAM) is reserved by the Operating System.
  • 12. Sequential Files Processing a FileProcessing a File  When a file is open it can be written towritten to or reador read from. (both in the case of random and binary files)  Writing to a file will save it to backingbacking store.store.
  • 13. Sequential FilesSequential Files Closing a fileClosing a file  When a file has been opened and processed it must then be closedmust then be closed.  The Operating system will then release the memory.release the memory.
  • 14. Visual Basic  VB supports all three file types, but you are only likely to use two of them  Text files  Random Access files
  • 16. Sequential/Text Files Input File opened for read-only access. Output File opened for output which is only write-to or create Append The file is opened for adding new data to an existing file. This is the default setting. Random The file is open for random access. This is writing or reading one record at a time. Binary The file is opened in binary mode
  • 17. Using the OpenFileDialog control Dim Filename as String OpenFileDialog1.ShowDialog() Filename= OpenFileDialog1.Filename lblFilename.Text = Filename
  • 18. Opening FilesOpening Files FileOpen(1, Filename, OpenMode.Input) ‘to read from the file FileOpen(1, Filename, OpenMode.Output) ‘to write to the file FileOpen(1, Filename, OpenMode.Append) ‘to write to the end of the file Note – 1 assigns the file the number 1. All files are identified by a number, not by their name. If you have two or more files open at once they must have different numbers.
  • 19. Opening Files  The FileOpen statement opens a file if it exists. When you open a file to read from it, an error results if it does not exists. When you open a file to write to it, if it doesn’t exist FileOpen first creates it and opens it.  Filename contains the name and path of the file
  • 20. Opening (creating) a Sequential File This algorithm would achieve this: 1.1. Enter FilenameEnter Filename 2.2. Open File for writingOpen File for writing 3.3. Input InformationInput Information 4.4. Save to fileSave to file 5.5. Close fileClose file
  • 21. Opening (creating) a Sequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file Filename= OpenFileDialog1.FileName
  • 22. Opening (creating) a Sequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file FileOpen(1, Filename, OpenMode.Output)
  • 23. Writeline(1, DataToBeWritten) Opening (creating) a Sequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file
  • 24. Opening (creating) a Sequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file FileClose (1)
  • 25. Opening a sequential fileOpening a sequential file The final code would look like: Dim Filename as string ‘File manipulation Program ‘Create File Private sub cmdCreateFile_Click() OpenFileDialog1.ShowDialog() Filename = FileDialog1.FileName FileOpen(1,Filename, OpenMode.Output) WriteLine(1,DataToBeWritten) FileClose (1) End Sub