从文件流中读取数据

L2740611032 2009-11-18 07:29:42
#include "iostream"
#include "string"
#include "fstream"
using namespace std;

class Text
{
public:
Text();
Text(string str);
~Text();
string contents();
private:
string str1;

};
Text::Text()
{
str1="hello";
}
Text::Text(string str)
{
ifstream input("str",ios_base::binary);

if(input.eof())
{
input.read(( char*)&str1, sizeof(str1)) ;
}

input.close();
}
string Text::contents ()
{
return str1;
}
Text::~Text ()
{
cout<<"Reference";
}
int main()
{
string str3;
Text text("hello.txt") ;
str3=text.contents ();
cout<<str3;
return 0;
}

为什么我得不到我要的结果呀 hello.txt 中存放的是hello
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
L2740611032 2009-11-18
  • 打赏
  • 举报
回复
哦 是不是要写上必要的文本信息啊!这个题是这样子的我是在thinking in++ 看到的 原题是这样的
create a Text class that contains a string object to hold the text of file.Give it two constructors :a default constructor and a constructor takes a string argument that is the name of the name of the file to open .when the second constructor is used,open the file and read
the contents into the string member object.Add a member function constents()to return
the string so it can be printed.In main(),open a file using Text and print the contents


请帮忙看一下,谢谢!
clhposs 2009-11-18
  • 打赏
  • 举报
回复
//[code=C/C++]写在这个里面,然后类里面自己注意命名规范
[/code]
L2740611032 2009-11-18
  • 打赏
  • 举报
回复
那该怎么规范下呢 给点建议嘛 谢谢!
clhposs 2009-11-18
  • 打赏
  • 举报
回复
代码

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧