qq_52812019
想做Dog的Monkey~
2021-01-15 20:30

c++,为什么文件读取进string失败,并且主函数返回值异常?

  • c++
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    string s;
    cout << "creat str!" << endl;
    fstream f("a.txt", ios::in|ios::binary);
    if (!f)
    {
        cout << "open file faild" << endl;
        return 1;
    }
    cout << "open file." << endl;
    f.read((char*)&s, sizeof(s));
    cout << "read."<< endl;
    cout << s << endl;
    return 0;
}

输出:

其他一切正常,其中a.txt内容:

 

 就是输出不了string s的内容.请问是为啥?怎么改?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答