Which also works perfectly well. Is there any way you can help me? Full source code, for review, is up on https://github.com/GBGamer/SLED推荐答案问题是char* filename只是指向某些包含字符的内存的指针.它本身不拥有任何内存.当您使用命令行参数时,程序会将该字符串存储在某处,并获得指向它的指针.当您尝试使用cin >> filename进行读取时,实际上没有任何地方可以存储读取的数据.The problem is that char* filename is just a pointer to some memory containing characters. It does not own any memory itself.When you use the command line argument, the program handles storing that string somewhere, and you get a pointer to it. When you try to read using cin >> filename there isn't actually anywhere to store the read data.解决方案:将char* filename替换为std::string filename(和#include <string>).Solution: Replace char* filename with std::string filename (and #include <string>).然后打开输出文件,您需要一个c样式的字符串(以空终止的char数组). std::string具有此功能.你会写Then to open the output file, you need a c-style string (null terminated char array). std::string has a function for this. You would writestd::ofstream out(filename.c_str()); ^^^^^或者,实际上,如果您可以使用具有c ++ 11功能的最新编译器,则甚至不需要使用c_str().添加了新的 std::ofstream构造函数以接受std::stringOr, in fact, if you can use a recent compiler with c++11 features, you don't even need to use c_str(). A new std::ofstream constructor has been added to accept a std::string. 这篇关于C ++:cin>> *字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!