+ All Categories
Home > Technology > CS215 - Lec 3 single record operations

CS215 - Lec 3 single record operations

Date post: 24-Dec-2014
Category:
Upload: arab-open-university-and-cairo-university
View: 394 times
Download: 3 times
Share this document with a friend
Description:
File organization course: operations for reading and writing a single record on disk
23
Transcript
Page 1: CS215 - Lec 3  single record operations
Page 2: CS215 - Lec 3  single record operations

� Writing and reading data back from a file.

� Extraction operator VS getline function.

� Write a single record on a file.

� Read a single record from a file.

� Build Company class.

� Overload operators for the Company class.

� Build template for programs.

Dr. Hussien M. Sharaf 2

Page 3: CS215 - Lec 3  single record operations

� What is the problem with reading data from files?

� Since we are reading from files in form of streams of bytes therefore we can not differentiate between tokens.

� Fortunately, the extraction operator in C++ can tokenize streams.

� But some fields could include more than one token.

Dr. Hussien M. Sharaf 3

Hussien M. Sharaf [email protected] CS215

Name EmailID Course

Page 4: CS215 - Lec 3  single record operations

Dr. Hussien M. Sharaf 4

Page 5: CS215 - Lec 3  single record operations

� ofstream: Stream class to write on files

� ifstream: Stream class to read from files

� fstream: Stream class to both read and write from/to files.

Dr. Hussien M. Sharaf 5

Page 6: CS215 - Lec 3  single record operations

� Write the previous data into a text file then try reading using:

1. Method: .get (buf,'\n');

2. Method: .getline (buf,'\n');

3. Function: getline (ifstream, string_Line,'\n');

Page 7: CS215 - Lec 3  single record operations

// Write a text file

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main () {

string line,token;

char buf[1000];

ofstream myfilePut("example.txt",ios::trunc);

if (myfilePut.is_open())

{

myfilePut<<"Hussien M. Sharaf\n";

}

Dr. Hussien M. Sharaf 7

myfilePut.close();ifstream myfileGet ("example.txt");if (myfileGet.is_open()){ while (! myfileGet.eof())

{//myfileGet >>token;myfileGet.get (buf,'\n');line=buf;myfileGet.seekg(0,ios::beg);myfileGet.getline (buf,'\n');line=buf;cout << line<< endl;}

Page 8: CS215 - Lec 3  single record operations

//clear flags any bad operation

myfileGet.clear();

myfileGet .seekg(0,ios::beg );

getline (myfileGet,line,'\n');

cout << line<< endl;

//clear flags any bad operation

myfileGet.clear();

myfileGet .seekg(0,ios::beg );

//use exttaction op to read into buf

while (! myfileGet.eof())

{

myfileGet>>buf;

cout << buf<< endl;

}

Dr. Hussien M. Sharaf 8

//clear flags any bad operationmyfileGet.clear();myfileGet .seekg(0,ios::beg );//use exttaction op to read into stringwhile (! myfileGet.eof()){

myfileGet>>line;cout << line<< endl;

}

myfileGet .close();}else cout << "Unable to open file"; system("Pause");return 0;}

Page 9: CS215 - Lec 3  single record operations

� Try reading using:

1. Extraction op.

2. Function: getline (ifstream,string_Line,'\n');

� myfileGet>>line;

� getline (myfileGet,line,'\n');

� Does the extraction op. identify separators other then space?

Page 10: CS215 - Lec 3  single record operations

Dr. Hussien M. Sharaf 10

� It was noticed that the extraction operator “>>” reads until first token separator [space, “,”, “;”, TAB, “\r” ]

� What are your suggestions for reading a collection of fields?

Page 11: CS215 - Lec 3  single record operations

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

string ExtendstringLength(string In, intrequiredSize, string appendchar)

{

while (In.length()<requiredSize )

In=In.append(appendchar);

return In;

}

int main () {

string line,token;

int ID;

string FullName,Course,Email;

char buf[1000];

ID=811;

Dr. Hussien M. Sharaf 11

Course="CS215";FullName ="Hussien M. Sharaf";Email="[email protected]";Course.append("@");FullName=ExtendstringLength(FullName,20,"@");Email = ExtendstringLength(Email,50,"@");ofstream myfilePut("example.txt",ios::trunc);if (myfilePut.is_open()){myfilePut<<ID<<Course<<FullName<<Email ;}myfilePut.close();

Page 12: CS215 - Lec 3  single record operations

ifstream myfileGet ("example.txt");if (myfileGet.is_open()){

myfileGet>>ID;cout << ID<< endl;myfileGet>>Course ;cout << Course << endl;//clear flags any bad

operationmyfileGet.clear();myfileGet .seekg(0,ios::beg

);myfileGet>>ID;cout << ID<< endl;

myfileGet.get( buf,7) ;Course=buf;

Dr. Hussien M. Sharaf 12

Course=Course.substr(0,5);cout << Course << endl;

myfileGet.get( buf,21) ;FullName=buf;FullName=FullName.erase(FullName.find("@"));cout << FullName<< endl;

myfileGet.get( buf,51) ;Email=buf;Email=Email.erase(Email.find("@"));cout << Email<< endl;

}system("Pause");return 0;

}

Page 13: CS215 - Lec 3  single record operations
Page 14: CS215 - Lec 3  single record operations

Dr. Hussien M. Sharaf 14

� What are the required fields? And what are their types?

1. CompanyName

2. ContactName

3. TitleofBusiness

4. Address

5. City

6. ZipCode

7. Phone

8. Fax

9. Email

10. WebSite

11. CompanyCode

12. CompanyDescription

Page 15: CS215 - Lec 3  single record operations

� Each field is qualified by double quotes “.

A screen shot of a file sample

Page 16: CS215 - Lec 3  single record operations

#include <iostream>using namespace std;class Company{

string CompanyCode, CompanyDescription ,CompanyName, ContactName, TitleofBusiness, Address,City, ZipCode, Phone, Fax, Email, WebSite;

};

1. Write default constructor, copy constructor.2. Overload “=” operator and “==” [optional]3. Overload “<<” and “>>” for ostream and istream. 4. Write a driver that uses the above class to read a single line that

contain all fields of a company. Each field is quoted by double quotes “field_Value” and separated from the next field by a comma ,

Example:"155","All",“A-z Maintenance",“Malek",“-","902 Bestel Avenue - Garden Grove“,..

Dr. Hussien M. Sharaf 16

Page 17: CS215 - Lec 3  single record operations

Dr. Hussien M. Sharaf 17

User Interface

Classes containing any processing of data

Page 18: CS215 - Lec 3  single record operations

//Declarations

while (ExitProgram!=true)

{ //take user choice

switch (UserChoice)

{ case 'I':

case 'i':

//handle user Choice

case'E':

case'e':ExitProgram=true; break;

}

}

system("pause");

Dr. Hussien M. Sharaf 18

Page 19: CS215 - Lec 3  single record operations

1. Start by determining Output.

2. List the inputs.

3. Think about processing.

Page 20: CS215 - Lec 3  single record operations

Check Ex 3:� Continue building a class for CompanyInfo:

1. Write default constructor, copy constructor.2. Overload “=” operator and “==” [optional]3. Overload “<<” and “>>” for ostream and istream and use

the delimiter method for reading and writing each field. Each field is required to be enclosed inside two double quotes i.e “…..”

� Write a driver to use this class based on the template Menu.

Dr. Hussien M. Sharaf 20

Page 21: CS215 - Lec 3  single record operations

� Next week is the deadline.

� No excuses.

� Don’t wait until last day.

� I can help you to the highest limit within the next 3 days.

Dr. Hussien M. Sharaf 21

Page 22: CS215 - Lec 3  single record operations

1. Delete the “bin” and “obj” folders.

2. Compress the solution folder using winrar.

3. Rename the compressed file as follows:

StudentName_ID_A3.rar

4. Email to: [email protected] your ID in the subject.

5. With subject: “FO – Assignment#3 -ID”

Dr. Hussien M. Sharaf 22

Page 23: CS215 - Lec 3  single record operations

Recommended