#Trying to convert a binary files input to hex. No idea why this isn't working...

48 messages · Page 1 of 1 (latest)

vernal carbon
#

Probably doing something stupid, if anyone can point me in the right direction it'd be appreciated. Not getting any output at all besides the logs that the binary file is loaded and the "press any key" prompt. Variables logged in my IDE (see screenshot) are logged at the std::cout << hexContent with a breakpoint added there.

#include "filesystem"
#include <fstream>
#include <vector>
#include <string>
#include <cassert>
#include <bitset>

int main()
{
    
    namespace fs = std::filesystem;
    fs::path f{"rom/rom.h"};
    std::ifstream::pos_type size;
    std::vector<uint8_t> memblock(size);
    
    //check if file exists
    if (fs::exists(f))
    {
        std::cout << "File exists" << std::endl;
        
        // Open Binary file
        std::ifstream input(f, std::ios::in | std::ios::ate | std::ios::binary);
        if (input.is_open())
        {
            size = input.tellg();
            input.seekg(0, std::ios::beg);
            input.read(reinterpret_cast<char*>(memblock.data()), size);
            input.close();
            
            std::cout << "The complete file is in memory" << std::endl;
            
            std::stringstream ss;
            for (int i = 0; i < size; ++i)
            {
                ss << std::hex << (int)memblock[i];
            }
            std::string hexContent = ss.str();
            std::cout << hexContent;
        }
    }
    else
    {
        std::cout << "File does not exist" << std::endl;
    }
    
    
    //prevents application from automatically closing and waiting for user-input. Debugging purposes.
    std::cout << "Press any key to exit." << std::endl;
    std::cin.ignore();
    std::cin.get();
    return 0;
}```
toxic inletBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

vernal carbon
#

Alright maybe I fucked up more than I thought

#

Figured out the size is actually always 0. Manually set the for loop to for (int i = 1; i < 100; ++i)

#

SIGSEGV segmentation faults now

#

probably fucking up something that should make common sense with the tell/seekg shiz

vernal orchid
#

For getting file size you shouldn't use tellg and seekg

#

especially because you have std::filesystem

#

!cppref file_size

toxic inletBOT
vernal orchid
#

also you're initializing your vector with a arbitrary size (uninitialized variable)

#

that's not good

vernal carbon
#

Doesn't fix anything though

#

size is still considered 0

vernal orchid
#

did you try running the code

#

sometimes the linter is wrong

vernal carbon
#

I did...

#

And also when I manually change the size to for example 10 I get those segmentation faults

#

so at this point... I'm just utterly confused at what the problem is here

#

been stumped on this for two hours now and I've been using CPP for way to long now lmao

vernal orchid
#

when you're stumped it's better to just put it aside for a bit lol

#

maybe someone kore knowledgeable than me picks this up

vernal carbon
#

I know, but this is just dumb

#

Everything should check out. I've done crazier shit than this for my job, and this isn't even for them...

vernal carbon
#

oh my god

#

fucking kill me

#

I think I found the issue

vernal carbon
#

!solved

toxic inletBOT
#

Thank you and let us know if you have any more questions!

vernal carbon
#

@vernal orchid Figured it out, thanks for your help though.

#

First off, my file was corrupted lmao, secondly the filesystem library didn't function properly. Did some syntax magic and it worked.

#

Seems like a CPP bug

vernal orchid
#

filesystem bug?

#

explain

#

a bug in the standard library is a big deal

#

glad you got it sorted out btw

vernal carbon
#

uh idk

#

yes it was a bug in the standard library

#

Saw some post on stackoverflow from 2 months ago detailing it so it's whatever, issue is already known

#

I got way to much on my hands fixing my own bugs, not really focused on those with the programming language itself

frigid charm
frigid charm
# vernal carbon uh idk

in your solution from above you cast char to int, completely ignoring the fact that char can be from -128 to 127, and directly dumping this value in the stringstream, the correct way to do this is to cast to unsigned char first (to get values from 0 to 255) and then print these to the stringstream with the std::hex format modifier

#

another problem is that you do not use ifstream::gcount() to see how many bytes have been read from the file when you call read, there is no guarantee that it will read the full contents of the file like you expect

#

a better implementation would be to keep calling read with the length until you trigger eof() meaning end of file, or you raise some error flag like fail() or bad() meaning "someone disconnected the USB used to read the file" or some other mysterious OS error