Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label write files in binary. Show all posts
Showing posts with label write files in binary. Show all posts

Find a particular person from file C++

C++ Program to find a particular person from a file using file handling.

1:  #include <iostream>  
2:  #include <fstream>  
3:  using namespace std;  
4:    
5:  /////////////////////////////////////////////////////////////////////////////////////////////////  
6:  class person{  
7:  protected:  
8:       char name[80];  
9:       short age;  
10:  public:  
11:       void getData(){  
12:            cout << "Enter name: "; cin.ignore(); cin.getline(name,80,'\n');   
13:            cout << "Enter age: "; cin >> age;   
14:       }  
15:       void showData(){  
16:            cout << "Name: " << name << endl;  
17:            cout << "Age: " << age << endl;  
18:       }  
19:  };  
20:  /////////////////////////////////////////////////////////////////////////////////////////////////  
21:  int main(){  
22:       person pers;  
23:       fstream fs("person.dat", ios::in | ios::out | ios::binary);  
24:       char opt;  
25:       do{  
26:            cout << "\nEnter person's data:\n";  
27:            pers.getData();  
28:            fs.write(reinterpret_cast<char*>(&pers),sizeof(person));  
29:            cout << "Any other person left? (y/n) "; cin >> opt;  
30:       }  
31:       while (opt == 'y');  
32:         
33:       cout << "\n\nWhich person's data do you want to get? "; int n; cin >> n;  
34:       fs.seekg(0,ios::end);  
35:       int endPosition = fs.tellp();  
36:       int totalPersons = endPosition/sizeof(person);  
37:       cout << "Total Persons are: " << totalPersons << endl;  
38:         
39:       fs.seekg((n-1)*sizeof(person));  
40:       fs.read(reinterpret_cast<char*>(&pers),sizeof(person));  
41:       pers.showData();  
42:         
43:       return 0;  
44:  }  

Write Objct to File C++

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

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void getData(){
cout << "Enter name: "; cin.getline(name,80,'\n');
cout << "Enter age: "; cin >> age;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;
pers.getData();

//open file.
ofstream os("PERSON.DAT", ios::binary);
//write this object to file.
os.write(reinterpret_cast<char*>(&pers), sizeof(person));
cout << "FILE WRITTEN" << endl;
return 0;
}

Now

Read Object from File C++

Write files in binary C++


Write files in binary in C++.

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

int main(){
int a[10];
for (int i = 0; i<10; i++){
a[i] = i;
}

ofstream os("REHAN.txt", ios::binary);
os.write(reinterpret_cast<char*>(a),10*sizeof(int));

os.close();
for (i = 0; i<10; i++){
a[i] = 0;
}

ifstream is("REHAN.txt",ios::binary);
is.read(reinterpret_cast<char*>(a),10*sizeof(int));
for (i = 0; i<10; i++){
cout << a[i] ;
}
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."