0

C++ object with fstream

I want to do method in class for read and show text from file.txt. in main i can cout this. but when i try to do the same on class with method. it doesn't print anythink from file.txt. do you know why ? can you help me with this ?

23rd Jan 2017, 5:24 AM
Piotr Koczeń
Piotr Koczeń - avatar
7 Respostas
0
would be nice if you could provide some code. it would help us to help you. ;)
23rd Jan 2017, 6:05 AM
malicious
0
void Postac::wyprawa(){ fstream plik; string linia; int x; srand(time(NULL)); x=rand()%9+1; switch(x){ case 1:{ plik.open("file1.txt",ios::in); if(plik.good()==true){ while(getline(plik,linia)){ cout<<linia;} plik.close(); } _getch(); break; } .... and other case the same but diferencename of file
23rd Jan 2017, 4:18 PM
Piotr Koczeń
Piotr Koczeń - avatar
0
please help me. i need it now ;(
23rd Jan 2017, 10:55 PM
Piotr Koczeń
Piotr Koczeń - avatar
0
whats the point your random variable? the switch also makes no Sense for me... btw u shouldnt close your stream in the while loop
23rd Jan 2017, 10:58 PM
malicious
0
random variable: 1 read text from file1.txt. 2 read text from file2.txt ... 9 read text from file9.txt close what?
23rd Jan 2017, 11:01 PM
Piotr Koczeń
Piotr Koczeń - avatar
0
you close your stream right after u read 1 line. put it outside of the while loop. also u can check the solo learn example: #include <iostream> #include <fstream> using namespace std; int main () { string line; ifstream MyFile("test.txt"); while ( getline (MyFile, line) ) { cout << line << '\n'; } MyFile.close(); }
23rd Jan 2017, 11:08 PM
malicious
0
ok. I know wherw i have mistake. the file.txt isnt in good location
23rd Jan 2017, 11:16 PM
Piotr Koczeń
Piotr Koczeń - avatar