+ All Categories
Home > Education > Cpp lab 13_pres

Cpp lab 13_pres

Date post: 07-Nov-2014
Category:
Upload: youth-for-peace
View: 2,057 times
Download: 1 times
Share this document with a friend
Description:
file handling
Popular Tags:
26
Lab - 13 IDE Microsoft Visual C++ 6.0 Sajid Ali Gillal OOP Object Oriented Programming
Transcript
Page 1: Cpp lab 13_pres

Lab - 13IDE

Microsoft Visual C++ 6.0

Sajid Ali Gillal

OOP

Object Oriented Programming

Page 2: Cpp lab 13_pres

File

Page 3: Cpp lab 13_pres

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.

Page 4: Cpp lab 13_pres

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.

Page 5: Cpp lab 13_pres

Random Access File&

Sequential Access File

File

Page 6: Cpp lab 13_pres
Page 7: Cpp lab 13_pres
Page 8: Cpp lab 13_pres

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.

Page 9: Cpp lab 13_pres

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.

Page 10: Cpp lab 13_pres

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.

Page 11: Cpp lab 13_pres

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.

Page 12: Cpp lab 13_pres

File Types

Text

Binary

Stream

Random-Access

File

common use

possible, butnot as common

Page 13: Cpp lab 13_pres

What You Will Learn

Create files

Read files

Write files

Update files

Page 14: Cpp lab 13_pres

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.

Page 15: Cpp lab 13_pres

How do I read an intfrom a file?

Page 16: Cpp lab 13_pres

"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.

Page 17: Cpp lab 13_pres

(“C:\\Documents and Settings\\01-113082-009\\Desktop\\ABC\\Rose.txt”);

Page 18: Cpp lab 13_pres

Create File P1.cpp

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

void main( ){

ofstream Savefile("D:\Rose.txt");

}

Page 19: Cpp lab 13_pres

(“C:\\Documents and Settings\\01-113082-009\\Desktop\\ABC\\Rose.txt”);

Page 20: Cpp lab 13_pres

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

Page 21: Cpp lab 13_pres

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";

}

Page 22: Cpp lab 13_pres

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

Page 23: Cpp lab 13_pres

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";

}

Page 24: Cpp lab 13_pres

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

Page 25: Cpp lab 13_pres

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

Page 26: Cpp lab 13_pres

OOP

Object Oriented Programming

Sajid Ali Gillal

May 21, 2010 26Sajid Ali Gillal


Recommended