输出文件流的“ <<”和“ put”之间的区别

问题描述

我试图理解将字符写入输出文件的'

我的代码:

#include <fstream>
using namespace std;

int main() {
    ofstream out ("output.txt");

    int x = 1;

    // This produces the incorrect result ...
    out.put(x);
    
    // ... while this produces the correct result
    out << x;


    // These two produce the same (correct) result
    out.put('a');
    out << 'a';
    
    out.close;
}

我知道out.put(x)会根据ASCII码将整数1转换为字符,但是我不明白为什么在使用out << x时不会出现这种情况。

但是,out.put('a') 确实产生的结果与out << 'a'相同。

这是为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)