Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //#include <fstream>
- #define nl '\n'
- // ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- using namespace std;
- class Student{
- public :
- int id , age ;
- char name[10];
- };
- void writeRecord(){
- Student s1;
- ofstream outFile("data.txt" , ios::out | ios::app );
- char ch ='Y';
- do{
- cout <<"Enter id : ";
- cin >> s1.id ;
- cout <<"Enter name : ";
- cin >> s1.name;
- cout <<"Enter age: ";
- cin >> s1.age;
- outFile.write( (char *)&s1 , sizeof(s1));
- cout <<"if you want to Enter new records ? (press Y) " << nl;
- cin >> ch ;
- }while(ch=='Y');
- outFile.close();
- }
- void readText(){
- char ch , str[10];
- ifstream in("test.txt" , ios::in);
- if(in.is_open()){
- in >> str ;
- cout << str << nl;
- in.get(ch) ;
- cout << ch << nl;
- // any thing to read , size
- // in.get( str , sizeof(str));
- in.get(str ,10);
- cout << str << nl;
- } else cout <<"the file not found\n";
- in.close();
- }
- void readRecord(){
- ifstream inFile("data.txt" , ios::in);
- if(inFile.is_open()){
- Student s2 ;
- cout <<"Id\tName\tAge\n\n";
- while(!inFile.eof()){
- inFile.read( (char*)&s2 , sizeof(s2));
- if(!inFile.eof())
- cout << s2.id << '\t'<< s2.name << '\t' << s2.age << nl;
- }
- inFile.close();
- }else cout <<"Not Found";
- }
- int main(){
- //writeRecord();
- readRecord();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement