Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Change the endianness when reading from a file

I'm trying to get better understanding of endianness when someone read a file.

The machine i'm using is little endian.

The code down below is supposed to read any file type.

But what if the file we are reading is in UTF-16BE encoding, should we after reading the whole file change the endianness?

I'm asking this becouse i'm planing on editing the content of the file and output it in console.

In case we should change the endianness, how can that be done?

Right now i'm reading the files like this:

std::ifstream file("/RANDOME/PATH/file.html", std::ios::in | std::ios::binary);

std::string result;

file.seekg(0, std::ios::end);   
result.reserve(t.tellg());
file.seekg(0, std::ios::beg);


result.assign((std::istreambuf_iterator<char>(file)),
            std::istreambuf_iterator<char>());


file.close();

I have no idea how to change the endianness from Big to little when reading a file. Can someone kindly show me step by step how that is done correctly? i'm only trying to learn. I know the file is using UTF-16BE encoding that is not a guess.

Comments