Advertisement
Zeinab_Hamdy

Untitled

Mar 12th, 2023
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#include <fstream>
  3. #define nl '\n'
  4. // ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  5.  
  6. using namespace std;
  7.  
  8. class Student{
  9. public :
  10.     int id , age ;
  11.     char name[10];
  12. };
  13.  
  14. void writeRecord(){
  15.  
  16.     Student s1;
  17.     ofstream outFile("data.txt" , ios::out | ios::app );
  18.     char ch ='Y';
  19.  
  20.    do{
  21.     cout <<"Enter id : ";
  22.     cin >> s1.id ;
  23.  
  24.     cout <<"Enter name : ";
  25.     cin >> s1.name;
  26.  
  27.     cout <<"Enter age: ";
  28.     cin >> s1.age;
  29.  
  30.     outFile.write( (char *)&s1 , sizeof(s1));
  31.  
  32.  
  33.     cout <<"if you want to Enter new records ? (press Y) " << nl;
  34.     cin >> ch ;
  35.  
  36.    }while(ch=='Y');
  37.  
  38.  
  39.     outFile.close();
  40.  
  41. }
  42.  
  43.  
  44. void readText(){
  45.  
  46. char ch , str[10];
  47.     ifstream in("test.txt" , ios::in);
  48.  
  49.     if(in.is_open()){
  50.  
  51.         in >> str ;
  52.         cout << str << nl;
  53.  
  54.         in.get(ch) ;
  55.         cout << ch << nl;
  56.  
  57.         // any thing to read , size
  58.        // in.get( str , sizeof(str));
  59.  
  60.         in.get(str ,10);
  61.         cout << str << nl;
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     } else cout <<"the file not found\n";
  68.  
  69.     in.close();
  70. }
  71.  
  72.  
  73. void readRecord(){
  74.  
  75.  ifstream inFile("data.txt" , ios::in);
  76.  
  77.     if(inFile.is_open()){
  78.  
  79.         Student s2 ;
  80.         cout <<"Id\tName\tAge\n\n";
  81.  
  82.         while(!inFile.eof()){
  83.  
  84.             inFile.read( (char*)&s2 , sizeof(s2));
  85.  
  86.             if(!inFile.eof())
  87.             cout << s2.id << '\t'<< s2.name << '\t' << s2.age << nl;
  88.         }
  89.  
  90.         inFile.close();
  91.  
  92.     }else cout <<"Not Found";
  93.  
  94.  
  95. }
  96.  
  97. int main(){
  98.  
  99. //writeRecord();
  100.  
  101. readRecord();
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement