注册 登录
编程论坛 C++教室

怎么把读入文档写在构造函数中?

bluegirlhere 发布于 2013-04-12 21:41, 375 次点击
fstream newsFile;
  newsFile.open("I:\\NewBrowserText.txt");
4 回复
#2
邓士林2013-04-12 22:06
给你个例子,只要在构造函数中写入即可:
void getshuju::duqu()
{ cout<<"输入i的数值 即总共的期数"; cin>>i;
 //char filename[20]="F:\\158\\158.txt";
 //   ifstream inf(filename,ios_base::in);
if(inf)     //文件打开成功
{for(int j=0;j<i*8;j++)   
 //读入八个整数inf>>a[j/8][j%8];}
else
{ cout << "open file fail" << endl; exit(1);}  }
#3
bluegirlhere2013-04-12 22:21
谢谢。我刚刚调的是这样的。成功了。
class txtRead{
private:
    fstream newsFile;
    int rows;
    int cols;
    char news;
public:
    void printTxt();
    void countNum();
    txtRead();
};

txtRead::txtRead(){
    newsFile.open("I:\\NewBrowserText.txt");
    if(!newsFile){
        cout<<"you get wrong file's address!"<<endl;
    }
}
#4
bluegirlhere2013-04-12 22:23
回复 楼主 bluegirlhere
谢谢
#5
bluegirlhere2013-04-12 22:25
回复 2楼 邓士林
谢谢o(≧v≦)o~~好棒
1