what is wrong with this program?

后端 未结 4 1431
时光说笑
时光说笑 2021-01-27 04:54
#include 
#include 
#include 

using namespace std;

int main() {
    string x;
    getline(cin,x);
    ofstream o(\"f:/demo         


        
4条回答
  •  长发绾君心
    2021-01-27 05:02

    You are treating an std::string like something that it is not. It's a complex object that, somewhere in its internals, stores characters for you.

    There is no reason to assume that a character array is at the start of the object (&x), and the sizeof the object has no relation to how many characters it may indirectly hold/represent.

    You're probably looking for:

    o.write(x.c_str(), x.length());
    

    Or just use the built-in formatted I/O mechanism:

    o << x;
    

提交回复
热议问题