C++ Rookie needs help to correct ofstream binary write problem

0

I'm writing a small command line program that writes a binary file. I use ofstream write (opened as a binary file) to output the file. The problem is that when I run the program, the resulting file does not match the data I presented to the write command.

My questions are:

  • What is wrong with my code?
  • How could I debug this?

Thanks and regards...Paul

Here are the details.

Limiting the output to 196 bytes, the output looks like this:

Text

If I add another 68 bytes to the output I get this:

Text

Note how the file has been corrupted at offset 0x00000010, with data overwritten and random data inserted. The problem can be recreated with this code:

#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);
}

I'm using Visual Studio 2019 Community Edition on Windows 10.

c++
ofstream
asked on Stack Overflow Feb 2, 2020 by Paul Offord • edited Feb 2, 2020 by Paul Offord

1 Answer

0

Well that is very frustrating. I was using Notepad++ with a hex viewer plugin to check the output on one machine and UltraEdit on the other. The problem is that Notepad++ randomly displays the content incorrectly, whereas UltrEdit shows the expected output.

So just to avoid all doubt, this is a problem with Notepad++ and not a problem with ofstream write. No real surprise I guess.

answered on Stack Overflow Feb 5, 2020 by Paul Offord

User contributions licensed under CC BY-SA 3.0