Cpp lab 13_pres
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,064
On Slideshare
2,061
From Embeds
3
Number of Embeds
1

Actions

Shares
Downloads
59
Comments
0
Likes
0

Embeds 3

http://www.slideshare.net 3

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. OOP Object Oriented Programming Lab - 13 IDE Microsoft Visual C++ 6.0 Sajid Ali Gillal
  • 2. File
  • 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. 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.
  • 7. 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.
  • 8. 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.
  • 9. 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.
  • 10. File Types Text File Binary Stream Random-Access common use possible, but not as common
  • 11. What You Will Learn Create files Write files Read files Update files
  • 12. 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.
  • 13. How do I read an int from a file?
  • 14. "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.
  • 15. (“C:Documents and Settings01-113082-009DesktopABCRose.txt”);
  • 16. Create File P1.cpp #include <fstream> #include <iostream> using namespace std; void main( ) { ofstream Savefile("D:Rose.txt"); }
  • 17. (“C:Documents and Settings01-113082-009DesktopABCRose.txt”);
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. OOP Object Oriented Programming Sajid Ali Gillal May 21, 2010 Sajid Ali Gillal 26