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

实现对data.txt文件的读取

旗开得胜LK 发布于 2015-06-06 10:29, 411 次点击
请大神求解,以类方式实现,类中包含数据读、数据写两个函数;
3 回复
#2
yangfrancis2015-06-06 12:01
class data
{
private:
   int MyData;
public:
   data(int n=0;){MyData=n;}
   void SetData(int n){MyData=n;}
   void OutPut()
   {
         fstream temptfile;
         temptfile.open("data.txt",ios::out);
         temptfile<<MyData<<"\n;
         temptfile.close();temptfile.clear();
   }
   void Input()
   {
         fstream temptfile;char tmpchr[]="";//定义用于读取信息的字符串
         temptfile.open("data.txt",ios::in);
         temptfile.getline(tmpchr,10,'\n');
         MyData=atoi(tmpchr);               //把读出的字符串转成自己需要的类型
         temptfile.close();temptfile.clear();
   }
}
#3
苍穹之舞2015-06-12 16:26
回复 2楼 yangfrancis
这里有几个成员函数?
#4
yangfrancis2015-06-12 17:52
含构造函数是四个,public中的四个
1