|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么不能保存名字??
- #include <iostream>
- #include <string>
- #include <fstream>
- class File
- {
- public:
- std::string neirong;
- std::string speaker;
- std::ofstream fileoutput;
-
- File();
- ~File();
- void infile();
- void inspeakerfile();
- bool write();
- };
- File::File()
- {
- fileoutput.open("1.txt",std::ios::app);
- }
- File::~File()
- {
- fileoutput.close();
- }
- void File::infile()
- {
- std::getline(std::cin,neirong);
-
- }
- void File::inspeakerfile()
- {
- std::getline(std::cin,speaker);
- }
- bool File::write()
- {
- if(fileoutput.is_open())
- {
- fileoutput<<neirong<<"|"<<speaker<<"\n";
- return true;
- }
- else
- {
- return false;
- }
- }
- int main()
- {
- File wirtefile;
- std::cout<<"请写一句名言!:\n";
- wirtefile.infile();
- std::cout<<"请写名言的作者!:\n";
- wirtefile.inspeakerfile();
- if(wirtefile.write())
- {
- std::cout<<"成功写入文件!\n";
- return 0;
- }
- else
- {
- std::cout<<"写入文件失败!\n";
- return -1;
- }
-
- }
复制代码
|
|