C++ Rookie 需要帮助来纠正 ofstream 二进制写入问题
C++ Rookie needs help to correct ofstream binary write problem
我正在编写一个可写入二进制文件的小型命令行程序。我使用 ofstream write(作为二进制文件打开)来输出文件。问题是当我 运行 程序时,生成的文件与我提供给写入命令的数据不匹配。
我的问题是:
- 我的代码有什么问题?
- 我该如何调试它?
感谢并问候...保罗
这是详细信息。
将输出限制为 196 个字节,输出如下所示:
如果我在输出中再添加 68 个字节,我会得到:
请注意文件在偏移量 0x00000010 处是如何损坏的,数据被覆盖并插入了随机数据。可以使用此代码重新创建问题:
#include <iostream>
#include <fstream>
#include <vector>
#include "Windows.h"
class PcapNgFile
{
public:
std::ofstream pcapng;
PcapNgFile(std::wstring fileName);
void pcapWrite();
void closePcapng() { pcapng.close(); };
};
PcapNgFile::PcapNgFile(std::wstring fileName)
: pcapng(fileName.c_str(), std::ios::binary | std::ios::out)
{
if (pcapng.is_open()) std::wcout << "Output file: " << fileName << std::endl;
}
void PcapNgFile::pcapWrite()
{
std::vector<uint8_t> dataOut1
{
0x0d, 0x0a, 0x0a, 0x0d, 0x70, 0x00, 0x00, 0x00, 0x4d, 0x3c, 0x2b, 0x1a, 0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x18, 0x00,
'M', 'i', 'c', 'r','o', 's', 'o', 'f', 't', ' ',
'W', 'i', 'n', 'd', 'o', 'w', 's', ' ', 'S', 'e', 'r', 'v', 'e', 'r',
0x04, 0x00, 0x2b, 0x00,
'M', 'i', 'c', 'r','o', 's', 'o', 'f', 't', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'e', 't', ' ',
'I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'o', 'n', ' ', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's',
' ', '8', '.', '5', 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x02, 0x00, 0x05, 0x00, 'B', 'a', 'b', 'e','l', 0x00, 0x00, 0x00, 0x03, 0x00, 0x13, 0x00,
'B', 'a', 'b', 'e', 'l', ' ', 'D', 'D', 'D', ' ', 'C', 'o', 'n', 'v', 'e', 'r', 't', 'e', 'r',
0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x86, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x62, 0x03, 0x00, 0x00, 0x62, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0b, 0xab, 0xe1,
0x00, 0x00, 0x00, 0x0b, 0xab, 0xe1,
0xba, 0xbe, 0x10, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x65, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x2c, 0x03, 0x00, 0x00
};
pcapng.write((char*)&(dataOut1[0]), dataOut1.size());
return;
}
int wmain(int argc, wchar_t** argv)
{
PcapNgFile* outFile = new PcapNgFile(L"c:\traces\bug.pcapng"); // output file
outFile->pcapWrite();
outFile->closePcapng();
return (0);
}
我在 Windows 10.Visual Studio 上使用 Visual Studio 2019 社区版。
嗯,这很令人沮丧。我使用带有十六进制查看器插件的 Notepad++ 在一台机器上检查输出,在另一台机器上使用 UltraEdit。问题是 Notepad++ 随机显示不正确的内容,而 UltrEdit 显示预期的输出。
所以为了避免所有疑问,这是 Notepad++ 的问题,而不是 ofstream 写入的问题。我想没有真正的惊喜。
我正在编写一个可写入二进制文件的小型命令行程序。我使用 ofstream write(作为二进制文件打开)来输出文件。问题是当我 运行 程序时,生成的文件与我提供给写入命令的数据不匹配。
我的问题是:
- 我的代码有什么问题?
- 我该如何调试它?
感谢并问候...保罗
这是详细信息。
将输出限制为 196 个字节,输出如下所示:
如果我在输出中再添加 68 个字节,我会得到:
请注意文件在偏移量 0x00000010 处是如何损坏的,数据被覆盖并插入了随机数据。可以使用此代码重新创建问题:
#include <iostream>
#include <fstream>
#include <vector>
#include "Windows.h"
class PcapNgFile
{
public:
std::ofstream pcapng;
PcapNgFile(std::wstring fileName);
void pcapWrite();
void closePcapng() { pcapng.close(); };
};
PcapNgFile::PcapNgFile(std::wstring fileName)
: pcapng(fileName.c_str(), std::ios::binary | std::ios::out)
{
if (pcapng.is_open()) std::wcout << "Output file: " << fileName << std::endl;
}
void PcapNgFile::pcapWrite()
{
std::vector<uint8_t> dataOut1
{
0x0d, 0x0a, 0x0a, 0x0d, 0x70, 0x00, 0x00, 0x00, 0x4d, 0x3c, 0x2b, 0x1a, 0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x18, 0x00,
'M', 'i', 'c', 'r','o', 's', 'o', 'f', 't', ' ',
'W', 'i', 'n', 'd', 'o', 'w', 's', ' ', 'S', 'e', 'r', 'v', 'e', 'r',
0x04, 0x00, 0x2b, 0x00,
'M', 'i', 'c', 'r','o', 's', 'o', 'f', 't', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'e', 't', ' ',
'I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'o', 'n', ' ', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's',
' ', '8', '.', '5', 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x02, 0x00, 0x05, 0x00, 'B', 'a', 'b', 'e','l', 0x00, 0x00, 0x00, 0x03, 0x00, 0x13, 0x00,
'B', 'a', 'b', 'e', 'l', ' ', 'D', 'D', 'D', ' ', 'C', 'o', 'n', 'v', 'e', 'r', 't', 'e', 'r',
0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x86, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x62, 0x03, 0x00, 0x00, 0x62, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0b, 0xab, 0xe1,
0x00, 0x00, 0x00, 0x0b, 0xab, 0xe1,
0xba, 0xbe, 0x10, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x65, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x2c, 0x03, 0x00, 0x00
};
pcapng.write((char*)&(dataOut1[0]), dataOut1.size());
return;
}
int wmain(int argc, wchar_t** argv)
{
PcapNgFile* outFile = new PcapNgFile(L"c:\traces\bug.pcapng"); // output file
outFile->pcapWrite();
outFile->closePcapng();
return (0);
}
我在 Windows 10.Visual Studio 上使用 Visual Studio 2019 社区版。
嗯,这很令人沮丧。我使用带有十六进制查看器插件的 Notepad++ 在一台机器上检查输出,在另一台机器上使用 UltraEdit。问题是 Notepad++ 随机显示不正确的内容,而 UltrEdit 显示预期的输出。
所以为了避免所有疑问,这是 Notepad++ 的问题,而不是 ofstream 写入的问题。我想没有真正的惊喜。