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 ?
7 ответов
0
would be nice if you could provide some code. it would help us to help you. ;)
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
0
please help me. i need it now ;(
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
0
random variable: 1 read text from file1.txt. 2 read text from file2.txt ... 9 read text from file9.txt
close what?
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();
}
0
ok. I know wherw i have mistake. the file.txt isnt in good location