Changes

Jump to: navigation, search

User:Ateremetskyi

4,699 bytes added, 21:53, 10 April 2012
no edit summary
-----------------------------------------------------------------------------------------------------------------------
Exception
#include <iostream>
using namespace std;
 
int main(void)
{
int x = 10;
 
try
{
while(x--)
{
if(x == 4) { throw x; }
 
cout << " pass" << x << endl ;
}
}
catch(int x)
{
cout << "got it >> " << x << " << " << endl;
}
 
return 0;
}
 
---
 
Pretty TXT stream ( WRITE )
FILE Datafile.txt
 
Hello, I am jack and I am
35
years old.
 
==
 
#include <iostream>
#include <fstream>
using namespace std;
 
int main(){
ofstream fout("datafile.txt");
fout<<"Hello, I am jack and I am "<<endl<<35<<endl<<"years old.\n";
return 0;
}
 
---------------
 
int main(){
ifstream fin("datafile.txt");
char str[100];
fin>>str;
cout<<str<<endl; // Hello,
fin.getline(str, 99, '\n');
cout<<str<<endl; // I am jack and I am
str[0] = fin.get();
cout<<str[0]; // 3
cout<<"TheEnd"<<endl;
return 0;
}
 
---
 
Interesting Custom print statement
#include <cstdio>
using namespace std;
 
class Output{
public:
Output& print(const char* str){
printf(str);
return *this;
}
Output& print(int a){
printf("%d", a);
return *this;
}
};
 
int main(){
Output fout;
fout.print("Hello, I am jack and I am ").print(35).print(" years old.\n");
return 0;
}
 
OR
 
 
#include <cstdio>
using namespace std;
 
class Output{
public:
void print(const char* str){
printf(str);
}
void print(int a){
printf("%d", a);
}
};
 
int main(){
Output fout;
fout.print("Hello, I am jack and I am ");
fout.print(35);
fout.print(" years old\n");
return 0;
}
 
---
 
Read the written in the prev example file
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
 
class Employee{
char _name[15];
char _lastname[30];
int _empno;
double _salary;
public:
Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
set(name, lastname, empno, salary);
}
void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
strcpy(_name, name);
strcpy(_lastname, lastname);
_empno = empno;
_salary = salary;
}
ostream& print(ostream& OS)const{
return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno
<<", Salary: "<<_salary;
}
virtual ~Employee(){
//print(cout)<<"is dead!!!!"<<endl;
}
};
ostream& operator<<(ostream& OS, const Employee& E){
return E.print(OS);
}
 
int main(){
Employee E;
fstream file("emp.bin",ios::in|ios::binary);
for(int i=0;i<5;i++){
file.read((char*)&E, sizeof(Employee));
cout<<E<<endl<<endl;
}
return 0;
}
 
 
OR
 
 
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
 
class Employee{
char _name[15];
char _lastname[30];
int _empno;
double _salary;
public:
Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
set(name, lastname, empno, salary);
}
void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
strcpy(_name, name);
strcpy(_lastname, lastname);
_empno = empno;
_salary = salary;
}
ostream& print(ostream& OS)const{
return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno
<<", Salary: "<<_salary;
}
virtual ~Employee(){
//print(cout)<<"is dead!!!!"<<endl;
}
};
ostream& operator<<(ostream& OS, const Employee& E){
return E.print(OS);
}
 
int main(){
Employee E;
fstream file("emp.bin",ios::in|ios::binary);
file.seekg((ios::off_type)0, ios::end);
int loc = file.tellg();
while(loc > 0 ){
loc -= sizeof(Employee);
file.seekg((ios::pos_type)loc);
file.read((char*)&E, sizeof(Employee));
cout<<E<<endl;
}
return 0;
}
 
---
 
Write classes to the file
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
 
class Employee{
char _name[15];
char _lastname[30];
int _empno;
double _salary;
public:
Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
set(name, lastname, empno, salary);
}
void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
strcpy(_name, name);
strcpy(_lastname, lastname);
_empno = empno;
_salary = salary;
}
ostream& print(ostream& OS)const{
return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno
<<", Salary: "<<_salary;
}
virtual ~Employee(){
//print(cout)<<"is dead!!!!"<<endl;
}
};
ostream& operator<<(ostream& OS, const Employee& E){
return E.print(OS);
}
 
int main(){
Employee E[5]={
Employee("John", "Doe", 1234,12345.67),
Employee("Jack", "Brown", 22345,212345.67),
Employee("Bill", "Red", 3234,32345.67),
Employee("Louis", "Clark", 41234,42345.67),
Employee("Homer", "Simpson", 5234,52345.67)
};
fstream file("emp.bin",ios::out|ios::binary);
for(int i=0;i<5;i++){
file.write((const char*)&E[i], sizeof(Employee));
}
return 0;
}
3
edits

Navigation menu