Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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++ - Fatal编程技术网

C++ 二进制文件输出末尾的额外字符

C++ 二进制文件输出末尾的额外字符,c++,C++,我有以下代码: #包括 #包括 使用名称空间std; int main(){ int data[4]={2,4,8,16}; std::fstream out_file=std::fstream(“my_file.dat”,std::ios::out | std::ios::binary); 对于(int i=0;i

我有以下代码:

#包括
#包括
使用名称空间std;
int main(){
int data[4]={2,4,8,16};
std::fstream out_file=std::fstream(“my_file.dat”,std::ios::out | std::ios::binary);
对于(int i=0;i<4;i++){
out_file.write((char*)和data[i],4);
}
out_file.close();
}
哪些产出:

 clang++-7 -pthread -std=c++17 -o main main.cpp
 ./main
 od my_file.dat 
0000000 000002 000000 000004 000000 000010 000000 000020 000000
0000020
 

请注意,在二进制文件的末尾有一个额外的“0000020”。为什么会这样?我怎样才能摆脱它?我想这是一个回车字符?但是我没有写字符串…

它是由
od
打印的文件偏移量。但是0000020处不应该有地址,对吗?使用:
od-t x1 my_file.dat
。您会注意到,
0000020
上没有数据,只是为了说明那里没有更多的数据。这并不能解决问题,但您不需要调用
out_file.close()
main()
的末尾。析构函数会这样做。