Upload
youth-for-peace
View
2.058
Download
1
Embed Size (px)
DESCRIPTION
file handling
Citation preview
Lab - 13IDE
Microsoft Visual C++ 6.0
Sajid Ali Gillal
OOP
Object Oriented Programming
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.
Random Access File&
Sequential Access File
File
Sequential FilesSequential file techniques provide a straightforward way to read and writefiles. Basic's sequential file commands manipulate text files: files of ASCIIcharacters 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
Binary
Stream
Random-Access
File
common use
possible, butnot as common
What You Will Learn
Create files
Read files
Write 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 intfrom 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 everyupdate to the file's content and metadata be writtensynchronously.
"rwd". Open for reading and writing and require that everyupdate to the file's content (but not metadata) bewritten synchronously.
(“C:\\Documents and Settings\\01-113082-009\\Desktop\\ABC\\Rose.txt”);
Create File P1.cpp
#include <fstream> #include <iostream> using namespace std;
void main( ){
ofstream Savefile("D:\Rose.txt");
}
(“C:\\Documents and Settings\\01-113082-009\\Desktop\\ABC\\Rose.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 20Sajid Ali Gillal
File output with strings or lines of output P4.cpp
May 21, 2010 21Sajid Ali Gillal
#include <fstream.h>
void main( ) {
ofstream outfile("E:\Rose.txt");
outfile << "This is first line of Gillal Program\n";outfile << "This is second line of Gillal Program\n";outfile << "This is third line of Gillal Program\n";
}
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 22Sajid Ali Gillal
file output with characters P5.cpp
May 21, 2010 23Sajid Ali Gillal
#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 written\n";
}
Reads person (full object) from disk P6.cpp#include <fstream.h>#include <iostream.h>#include <stdio.h>#include <conio.h>
class person{protected:char name[80]; //person's nameshort age; //person's age
public:void showData( ) //display person's data{cout << "Name: " << name << endl;cout << "Age: " << age << endl;
}};
void main( ){
person pers; //create person variableFILE *ptr;ptr = fopen("E:\data2.txt","w");fread(&pers,sizeof(pers),1,ptr);pers.showData();getch();
}
May 21, 2010 24Sajid Ali Gillal
Save person (full object) to disk P7.cpp#include <fstream.h>#include <iostream.h>#include <stdio.h>
class person{protected:char name[80]; //person's nameshort age; //person's agepublic:void getData() //get person's data
{cout << "Enter name: ";cin >> name;cout << "Enter age: ";cin >> age;
}};
void main( ){person pers; //create a personpers.getData(); //get data for person
FILE *ptr;ptr = fopen("E:\Rose.dat","wb");fwrite(&pers,sizeof(pers),1,ptr);fclose(ptr);}
May 21, 2010 25Sajid Ali Gillal
OOP
Object Oriented Programming
Sajid Ali Gillal
May 21, 2010 26Sajid Ali Gillal