Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用;putchar“;以二进制形式输出一个字_C++_Binary_Bit Manipulation - Fatal编程技术网

C++ C++;使用;putchar“;以二进制形式输出一个字

C++ C++;使用;putchar“;以二进制形式输出一个字,c++,binary,bit-manipulation,C++,Binary,Bit Manipulation,我的职能: void output(int word) { file << putchar((word >> 24) & 0xff); file << putchar((word >> 16) & 0xff); file << putchar((word >> 8) & 0xff); file << putchar(word &

我的职能:

void output(int word)
{   
      file << putchar((word >> 24) & 0xff);
      file << putchar((word >> 16) & 0xff);
      file << putchar((word >> 8) & 0xff);
      file << putchar(word & 0xff);
}
void输出(int字)
{   
文件>24)和0xff);
文件>16)和0xff);
文件>8)&0xff);

文件您的错误是试图使用文本模式函数以二进制形式写入。您应该使用
file::put(char)
。如下所示:

void output(int word)
{   
    file.put(word >> 24);
    file.put(word >> 16);
    file.put(word >> 8);
    file.put(word);
}
由于
file.put
只接受
char
,您不需要使用0xff来计算结果,因此我将其忽略


另外,我不明白为什么要使用
putchar()
。它只打印到控制台,而不打印到文件。

问题是
运算符使用
无符号int-word
,然后告诉我您得到了什么。二进制输出保持不变。如何打开文件进行写入?全局变量:ofstream file;在main中:file.open(“binary.bin”);由函数使用,然后是:file.close();使用
file.open(“binary.bin”,ios::binary);
在那里。
  file << putchar((word >> 24) & 0xff);
       ^^----> this is the problem.