mardi 3 mars 2020

how to read all data in file

i want to read all data from file in form name depth class and go to next line and so on to end file so how i using visual c++ this my code tha have error in while caz i dont know how read all data in file

#include<string> 
#include<iostream> 
using namespace std; 
int main() 
{ 
    std::string data; 
    std::string data2; 
    std::string data3; 
    std::string out;
    std::ofstream MyFile; 
    MyFile.open("hh.txt",std::ios::app|std::ios::out);
    if(MyFile.fail()){
        cout<<"error"<<endl;
    }
    cout<<"plz enter ur name \n";   
   cout<<"plz enter ur dept \n"; 
   cout<<"plz enter ur class \n "; 
     while ( (std::getline(std::cin, data) && data!="exit") && (std::getline(std::cin, data2) && data2!="exit") && (std::getline(std::cin, data3) && data3!="exit") )
    {
   MyFile << data << "\t" << data2 << "\t" << data3 << endl;
   cout<<"plz enter ur name \n";    
   cout<<"plz enter ur dept \n"; 
   cout<<"plz enter ur class \n "; 
     }
    // MyFile<<std::getline(std::cout,out);
     //cout<<out;
      while ( !MyFile.eof() ) { 
          MyFile >>out;
      cout << out<< endl;

   }
      MyFile.close(); 
    return 0; }

Aucun commentaire:

Enregistrer un commentaire