Thread: std::ofstream strange behavior when writing binary

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    11

    std::ofstream strange behavior when writing binary

    Hi,
    I have a code that outputs the content of an array of std::maps in binary format.

    Code:
    auto outfile = std::fstream(output, std::ios::out | std::ios::binary);
    MatchedPair tmp;
    for (int i = 0; i < sizeArray; ++i)
    { 
        for (auto itr = arr[i].cbegin(); itr != arr[i].cend(); ++itr)
        {   
            tmp.ID2 = itr_in->first;
            tmp.distance = itr_in->second;
            outfile.write((char*)&tmp, sizeof(MatchedPair));
        }   
    } 
    outfile.close();
    The amount of maps depends on an external parameter, but it should always output the same binary file (the total amount of data is always the same).

    The problem I am having is that the outputs in binary do not much, but when I translate them into txt they are actually the same!!! I am using bash cmp utility to do the check.

    When comparing the binary, the difference cmp it finds is the following:

    Code:
     output_1.bin output_2.bin differ: byte 3473657, line 8763 is  15 ^M  16 ^N
    I honestly don't know the source of the difference and why when converting them back to txt they are actually the same .

    In any case, thanks for any help you can give me!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is MatchedPair?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    11
    The MatchedPair is a struct:
    Code:
    struct MatchedPair
    {
        uint32_t ID2;
        double distance;
        MatchedPair(): ID2(0), distance(0) {}
        MatchedPair(uint32_t id2, double d):  ID2(id2), distance(d) {}
    };

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    11
    I found out what the error was. It was actually a rounding error that the .txt file didn't show because it was truncating the results.
    So, as usual, it was 100% my mistake .
    In any case thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behavior in getline based writing to file.
    By dozerman in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2013, 08:46 AM
  2. Strange behavior
    By onako in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 07:00 AM
  3. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  4. ofstream - strange exit from program
    By addle_brains in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2007, 12:34 AM
  5. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM

Tags for this Thread