Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label file handling. Show all posts
Showing posts with label file handling. Show all posts

Printing random lines from file C++

Printing random lines from file in C++ program is given below:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main(){
ofstream os("a.txt");
for (int i = 0; i<=999; i++){
os << i << endl;
}
os.close();

ifstream is("a.txt");

char c;
string line;
while (!is.eof()){
while (c != '\n'){
is.seekg(rand()%1000);
c = is.get();
}
getline(is,line);
cout << line << endl; //Printing lines randomly from file.
c = '0';
Sleep(300);
}
return 0;
}


Though lines are being printed randomly but there is repetition.

Reading Random lines from a text file C++


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++

Read Object from File C++

After an object has been written in a file, you can read that object from file in C++.
I've written an object at Write Objct to File C++.

After writing this object, following is the program to read an object from a file.

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

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void showData(){
cout << "Name: " << name << endl;
cout << "Age: " <<  age << endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;

//open file.
ifstream is("PERSON.DAT", ios::binary);
//read an object from this file.
is.read(reinterpret_cast<char*>(&pers), sizeof(person));
//after reading object from file, Show it.
pers.showData();
return 0;
}

Write and read Object in file C++


Write and read Object in file in C++.

#include <iostream>
#include <fstream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void getData(){
cout << "Enter name: "; cin >> name;
cout << "Enter age: "; cin >> age;
}
void showData(){
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
////////////////////////////////////////////////////////////////////////////////////////////
int main(){
person pers;
pers.getData();
ofstream os("person.txt", ios::binary);//Open file in binary mode.
cout << "This data has been store in the file person.txt in directory you've created your project" << endl;
cout << "Now Enter this data again, this data will not be saved" << endl;
os.write(reinterpret_cast<char*>(&pers), sizeof(pers));
pers.getData();
os.close();

//PRINTING SAVED DATA IN FILE.
ifstream is("person.txt",ios::binary);
is.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showData();//Here the data saved in file should have been displayed, but it's showing data not saved in file. WHY?
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."