0

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 that 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::fstream 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 "; 
     }
      while ( std::getline(MyFile, out) )
      cout << out<< endl;
       }
      MyFile.close(); 
    return 0; }
youngah
  • 13
  • 5
  • 1
    This is not the primary bug but an important read: `while ( !MyFile.eof() ) { ` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Mar 03 '20 at 14:47
  • Related https://stackoverflow.com/questions/18034433/how-to-read-the-entire-content-of-a-file-character-by-character – RamblinRose Mar 03 '20 at 14:48
  • 3
    In C++ there's really no difference between streams. If you can read from one input stream (like `std::cin`) you can read from *all* input streams. Of course, it requires that you *have* an input stream to read from, and `MyFile` is not an input stream. – Some programmer dude Mar 03 '20 at 14:49
  • The "o" in "ofstream" is for "output". (This is why specifying `ios::out` for an `ofstream` is pointless.) – molbdnilo Mar 03 '20 at 15:01
  • i write std::cin,out; but it doesn't work... – youngah Mar 03 '20 at 15:05
  • You used an `ofstream` for your file. Are you trying to read or write to the file or read from cin? Your code appears to want to write to a file then read at the end. – drescherjm Mar 03 '20 at 15:08
  • yes i want to write and in the end read all data thats in file – youngah Mar 03 '20 at 15:12
  • Instead of putting all your code in `int main()`, you may want to separate the input and output parts of your program into 2 different functions and open the file in those functions. – drescherjm Mar 03 '20 at 16:14
  • One problem with your updated code is after writing the stream is at the end of the file so there is nothing to read. The second problem is in my first comment. Also you probably want to use getline() unless you want to output 1 word per line. – drescherjm Mar 03 '20 at 16:17
  • ok what should i write in the condition to read all data? – youngah Mar 03 '20 at 16:28
  • The code will be similar to what you already have with your `while ( (std::getline(` – drescherjm Mar 03 '20 at 16:31
  • Here is an example on how to read a file line by line: [https://stackoverflow.com/a/7868986/487892](https://stackoverflow.com/a/7868986/487892) – drescherjm Mar 03 '20 at 16:36
  • look i edited the code , but is still doesn't work – youngah Mar 03 '20 at 16:55
  • The updated code still has the problem that the stream is at the end when you try to read so there is nothing to read. – drescherjm Mar 03 '20 at 16:55
  • You may be interested in this: [https://stackoverflow.com/questions/5343173/returning-to-beginning-of-file-after-getline](https://stackoverflow.com/questions/5343173/returning-to-beginning-of-file-after-getline) – drescherjm Mar 03 '20 at 16:57
  • i cant understood TT – youngah Mar 03 '20 at 17:13
  • Again after you have written to the file the stream is at the end. Attempting to read at that point will return no data because you are at the end of the file. So you need to reset to the beginning of the file to read. The last link I posted did exactly that reset to the beginning so you can read all the data in the file. With all of this said I still stand by my comment that you should separate the input part of your program and the output part into separate functions opening the file separately in these 2 functions and you would not have had this issue. – drescherjm Mar 03 '20 at 17:32

0 Answers0