SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
OOP
       Object Oriented Programming


            Lab - 13
IDE
   Microsoft Visual C++ 6.0


                           Sajid Ali Gillal
File
Files
• A file is a collection of data in mass storage.
• A data file is not a part of a program’s source
  code.
• The same file can be read or modified by
  different programs.
• The program must be aware of the format of
  the data in the file.
Files (cont’d)
• The file system is maintained by the
  operating system.
• The system provides commands and/or GUI
  utilities for viewing file directories and for
  copying, moving, renaming, and deleting
  files.
• The system also provides “core” functions,
  callable from programs, for reading and
  writing directories and files.
File
 Random Access File
          &
Sequential Access File
Sequential Files
Sequential file techniques provide a straightforward way to read and write
files. Basic's sequential file commands manipulate text files: files of ASCII
characters with carriage-return/linefeed pairs separating records.

In computer science, sequential access means that a group of elements (e.g.
data in a memory array or a disk file or on a tape) is accessed in a
predetermined, ordered sequence. Sequential access is sometimes the only
way of accessing the data, for example if it is on a tape. It may also be the
access method of choice, for example if we simply want to process a
sequence of data elements in order.
Random Access Files
Random access files consist of records that can be accessed in any sequence.
This means the data is stored exactly as it appears in memory, thus saving
processing time (because no translation is necessary) both in when the file is
written and in when it is read.


In computer science, random access (sometimes called direct access) is the
ability to access an arbitrary element of a sequence in equal time.
Random-Access Files
• A program can start reading or writing a
  random-access file at any place and read or
  write any number of bytes at a time.
• “Random-access file” is an abstraction: any
  file can be treated as a random-access file.
• You can open a random-access file both for
  reading and writing at the same time.
Random-Access Files (cont’d)

• A binary file containing fixed-length data
  records is suitable for random-access
  treatment.
• A random-access file may be accompanied
  by an “index” (either in the same or a
  different file), which tells the address of each
  record.
File Types


            Text
File                  Binary




           Stream
                   Random-Access


                                 common use
                                 possible, but
                               not as common
What You Will Learn




        Create files
                       Write files

Read files




  Update files
Random Access Files
 A RandomAccessFile employs an internal pointer that points to the next
  byte to read.
 This pointer is zero-based and the first byte is indicated by index 0.
 When first created, a RandomAccessFile points to the first byte.
 You can change the pointer's position by invoking the different methods.
 The skipBytes method moves the pointer by the specified number of
  bytes.
 If offset number of bytes would pass the end of file, the internal pointer
  will only move to as much as the end of file.
How do I read an int
from a file?
"r".    Open for reading only.

"rw“.   Open for reading and writing.
          If the file does not already exist, Random Access
          File creates the file.

"rws". Open for reading and writing and require that every
       update to the file's content and metadata be written
       synchronously.

"rwd". Open for reading and writing and require that every
       update to the file's content (but not metadata) be
       written synchronously.
(“C:Documents and Settings01-113082-009DesktopABCRose.txt”);
Create File                                P1.cpp



     #include <fstream>
     #include <iostream>
     using namespace std;

     void main( )
     {
       ofstream Savefile("D:Rose.txt");


     }
(“C:Documents and Settings01-113082-009DesktopABCRose.txt”);
Create File                                      Q1.cpp



       #include <fstream>
       #include <iostream>
       using namespace std;

       void main( )
       {
         ofstream Savefile("D:Rose.txt");

           Savefile<< "Sajid Ali Gillal";

       }



 May 21, 2010                 Sajid Ali Gillal      20
File output with strings or lines of output              P4.cpp


#include <fstream.h>

void main( )
{
      ofstream outfile("E:Rose.txt");

          outfile << "This is first line of Gillal Programn";
          outfile << "This is second line of Gillal Programn";
          outfile << "This is third line of Gillal Programn";

}




 May 21, 2010                 Sajid Ali Gillal              21
File input with characters                      P2.cpp


     #include <fstream>
     #include <iostream>

     using namespace std;

     void main( )
     {
           char ch;
           ifstream Readfile("D:Rose.txt");
           while(Readfile)
           {
                  Readfile.get(ch);
                  cout << ch;
           }
           cout << endl;

     }
 May 21, 2010                Sajid Ali Gillal     22
file output with characters                                      P5.cpp

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void main( )
{
      string str = "If you start judging the people then
                                   you will have no time to love them!";
            ofstream Savefile("E:Rose.txt");

            Savefile<<str;
            cout << "File writtenn";

}
    May 21, 2010               Sajid Ali Gillal                     23
Reads person (full object) from disk                                        P6.cpp
#include <fstream.h>                        void main( )
#include <iostream.h>                       {
#include <stdio.h>
#include <conio.h>                          person pers;     //create person variable
                                                    FILE *ptr;
class person                                        ptr = fopen("E:data2.txt","w");
{                                                   fread(&pers,sizeof(pers),1,ptr);
protected:                                          pers.showData();
char name[80]; //person's name                      getch();
short age;     //person's age
                                            }
public:
void showData( ) //display person's data
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
        }
};
   May 21, 2010                      Sajid Ali Gillal                           24
Save person (full object) to disk                                          P7.cpp
#include <fstream.h>                       void main( )
#include <iostream.h>                      {
#include <stdio.h>                         person pers; //create a person
                                           pers.getData(); //get data for person
class person
{                                          FILE *ptr;
protected:                                 ptr = fopen("E:Rose.dat","wb");
char name[80];      //person's name        fwrite(&pers,sizeof(pers),1,ptr);
short age;          //person's age         fclose(ptr);
public:                                    }
void getData()    //get person's data
        {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
        }
};
   May 21, 2010                     Sajid Ali Gillal                           25
OOP


      Object Oriented Programming




                                    Sajid Ali Gillal

May 21, 2010     Sajid Ali Gillal                      26

Contenu connexe

Tendances

Huong dan cai dat hadoop
Huong dan cai dat hadoopHuong dan cai dat hadoop
Huong dan cai dat hadoop
Quỳnh Phan
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
itsmesrl
 

Tendances (20)

Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
Redis/Lessons learned
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learned
 
File system
File systemFile system
File system
 
Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics
 
Large scale nlp using python's nltk on azure
Large scale nlp using python's nltk on azureLarge scale nlp using python's nltk on azure
Large scale nlp using python's nltk on azure
 
Huong dan cai dat hadoop
Huong dan cai dat hadoopHuong dan cai dat hadoop
Huong dan cai dat hadoop
 
Hawk presentation
Hawk presentationHawk presentation
Hawk presentation
 
Cscope and ctags
Cscope and ctagsCscope and ctags
Cscope and ctags
 
Swift 4 : Codable
Swift 4 : CodableSwift 4 : Codable
Swift 4 : Codable
 
What's new in Redis v3.2
What's new in Redis v3.2What's new in Redis v3.2
What's new in Redis v3.2
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 
Tutorial on Node File System
Tutorial on Node File SystemTutorial on Node File System
Tutorial on Node File System
 
Full Text search in Django with Postgres
Full Text search in Django with PostgresFull Text search in Django with Postgres
Full Text search in Django with Postgres
 
Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
2015 bioinformatics python_strings_wim_vancriekinge
2015 bioinformatics python_strings_wim_vancriekinge2015 bioinformatics python_strings_wim_vancriekinge
2015 bioinformatics python_strings_wim_vancriekinge
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
 
Object Storage with Gluster
Object Storage with GlusterObject Storage with Gluster
Object Storage with Gluster
 
Tajo Seoul Meetup-201501
Tajo Seoul Meetup-201501Tajo Seoul Meetup-201501
Tajo Seoul Meetup-201501
 
A Brief Introduction to Redis
A Brief Introduction to RedisA Brief Introduction to Redis
A Brief Introduction to Redis
 

En vedette

Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3
a_akhavan
 
Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.net
Girija Muscut
 
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-uPython Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Nikola Plejic
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15
Niit Care
 
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsPioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Wolfgang Stock
 

En vedette (20)

Presentation1
Presentation1Presentation1
Presentation1
 
Part 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative valuePart 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative value
 
Cognitive information science
Cognitive information scienceCognitive information science
Cognitive information science
 
Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Debugging in visual studio (basic level)
Debugging in visual studio (basic level)
 
Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3
 
Part2 database connection service based using vb.net
Part2 database connection service based using vb.netPart2 database connection service based using vb.net
Part2 database connection service based using vb.net
 
Part 1 picturebox using vb.net
Part 1 picturebox using vb.netPart 1 picturebox using vb.net
Part 1 picturebox using vb.net
 
Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.net
 
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-uPython Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
 
Making Information Usable: The Art & Science of Information Design
Making Information Usable: The Art & Science of Information DesignMaking Information Usable: The Art & Science of Information Design
Making Information Usable: The Art & Science of Information Design
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15
 
What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++
 
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsPioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Information Overload and Information Science / Mieczysław Muraszkiewicz
Information Overload and Information Science / Mieczysław MuraszkiewiczInformation Overload and Information Science / Mieczysław Muraszkiewicz
Information Overload and Information Science / Mieczysław Muraszkiewicz
 
How Not To Be Seen
How Not To Be SeenHow Not To Be Seen
How Not To Be Seen
 
Logical Programming With ruby-prolog
Logical Programming With ruby-prologLogical Programming With ruby-prolog
Logical Programming With ruby-prolog
 
Part 3 binding navigator vb.net
Part 3 binding navigator vb.netPart 3 binding navigator vb.net
Part 3 binding navigator vb.net
 
Transforming the world with Information technology
Transforming the world with Information technologyTransforming the world with Information technology
Transforming the world with Information technology
 
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
 

Similaire à Cpp lab 13_pres

Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
Princess Sam
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 

Similaire à Cpp lab 13_pres (20)

File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
working with files
working with filesworking with files
working with files
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File management
File managementFile management
File management
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Files in c++
Files in c++Files in c++
Files in c++
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 
C programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxC programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptx
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
File management in C++
File management in C++File management in C++
File management in C++
 

Dernier

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Cpp lab 13_pres

  • 1. OOP Object Oriented Programming Lab - 13 IDE Microsoft Visual C++ 6.0 Sajid Ali Gillal
  • 3. Files • A file is a collection of data in mass storage. • A data file is not a part of a program’s source code. • The same file can be read or modified by different programs. • The program must be aware of the format of the data in the file.
  • 4. Files (cont’d) • The file system is maintained by the operating system. • The system provides commands and/or GUI utilities for viewing file directories and for copying, moving, renaming, and deleting files. • The system also provides “core” functions, callable from programs, for reading and writing directories and files.
  • 5. File Random Access File & Sequential Access File
  • 6.
  • 7.
  • 8. Sequential Files Sequential file techniques provide a straightforward way to read and write files. Basic's sequential file commands manipulate text files: files of ASCII characters with carriage-return/linefeed pairs separating records. In computer science, sequential access means that a group of elements (e.g. data in a memory array or a disk file or on a tape) is accessed in a predetermined, ordered sequence. Sequential access is sometimes the only way of accessing the data, for example if it is on a tape. It may also be the access method of choice, for example if we simply want to process a sequence of data elements in order.
  • 9. Random Access Files Random access files consist of records that can be accessed in any sequence. This means the data is stored exactly as it appears in memory, thus saving processing time (because no translation is necessary) both in when the file is written and in when it is read. In computer science, random access (sometimes called direct access) is the ability to access an arbitrary element of a sequence in equal time.
  • 10. Random-Access Files • A program can start reading or writing a random-access file at any place and read or write any number of bytes at a time. • “Random-access file” is an abstraction: any file can be treated as a random-access file. • You can open a random-access file both for reading and writing at the same time.
  • 11. Random-Access Files (cont’d) • A binary file containing fixed-length data records is suitable for random-access treatment. • A random-access file may be accompanied by an “index” (either in the same or a different file), which tells the address of each record.
  • 12. File Types Text File Binary Stream Random-Access common use possible, but not as common
  • 13. What You Will Learn Create files Write files Read files Update files
  • 14. Random Access Files  A RandomAccessFile employs an internal pointer that points to the next byte to read.  This pointer is zero-based and the first byte is indicated by index 0.  When first created, a RandomAccessFile points to the first byte.  You can change the pointer's position by invoking the different methods.  The skipBytes method moves the pointer by the specified number of bytes.  If offset number of bytes would pass the end of file, the internal pointer will only move to as much as the end of file.
  • 15. How do I read an int from a file?
  • 16. "r". Open for reading only. "rw“. Open for reading and writing. If the file does not already exist, Random Access File creates the file. "rws". Open for reading and writing and require that every update to the file's content and metadata be written synchronously. "rwd". Open for reading and writing and require that every update to the file's content (but not metadata) be written synchronously.
  • 18. Create File P1.cpp #include <fstream> #include <iostream> using namespace std; void main( ) { ofstream Savefile("D:Rose.txt"); }
  • 20. Create File Q1.cpp #include <fstream> #include <iostream> using namespace std; void main( ) { ofstream Savefile("D:Rose.txt"); Savefile<< "Sajid Ali Gillal"; } May 21, 2010 Sajid Ali Gillal 20
  • 21. File output with strings or lines of output P4.cpp #include <fstream.h> void main( ) { ofstream outfile("E:Rose.txt"); outfile << "This is first line of Gillal Programn"; outfile << "This is second line of Gillal Programn"; outfile << "This is third line of Gillal Programn"; } May 21, 2010 Sajid Ali Gillal 21
  • 22. File input with characters P2.cpp #include <fstream> #include <iostream> using namespace std; void main( ) { char ch; ifstream Readfile("D:Rose.txt"); while(Readfile) { Readfile.get(ch); cout << ch; } cout << endl; } May 21, 2010 Sajid Ali Gillal 22
  • 23. file output with characters P5.cpp #include <fstream> #include <iostream> #include <string> using namespace std; void main( ) { string str = "If you start judging the people then you will have no time to love them!"; ofstream Savefile("E:Rose.txt"); Savefile<<str; cout << "File writtenn"; } May 21, 2010 Sajid Ali Gillal 23
  • 24. Reads person (full object) from disk P6.cpp #include <fstream.h> void main( ) #include <iostream.h> { #include <stdio.h> #include <conio.h> person pers; //create person variable FILE *ptr; class person ptr = fopen("E:data2.txt","w"); { fread(&pers,sizeof(pers),1,ptr); protected: pers.showData(); char name[80]; //person's name getch(); short age; //person's age } public: void showData( ) //display person's data { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; May 21, 2010 Sajid Ali Gillal 24
  • 25. Save person (full object) to disk P7.cpp #include <fstream.h> void main( ) #include <iostream.h> { #include <stdio.h> person pers; //create a person pers.getData(); //get data for person class person { FILE *ptr; protected: ptr = fopen("E:Rose.dat","wb"); char name[80]; //person's name fwrite(&pers,sizeof(pers),1,ptr); short age; //person's age fclose(ptr); public: } void getData() //get person's data { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; } }; May 21, 2010 Sajid Ali Gillal 25
  • 26. OOP Object Oriented Programming Sajid Ali Gillal May 21, 2010 Sajid Ali Gillal 26