How to read the binary file in C++ - c++

I want to write a program that opens the binary file and encrypts it using DES.
But how can I read the binary file?

"how can I read the binary file?"
If you want to read the binary file and then process its data (encrypt it, compress, etc.), then it seems reasonable to load it into the memory in a form that will be easy to work with. I recommend you to use std::vector<BYTE> where BYTE is an unsigned char:
#include <fstream>
#include <vector>
typedef unsigned char BYTE;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
with this function you can easily load your file into the vector like this:
std::vector<BYTE> fileData = readfile("myfile.bin");
Hope this helps :)

Related

Detect and Read Entire UTF-8 File using C++11?

I know of this traditional way,
#include <fstream>
#include <string>
#include <cerrno>
#include <iostream>
int main()
{
std::ifstream in("file.txt", std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize((size_t)in.tellg()); // Allocate buffer
in.seekg(0, std::ios::beg);
// Read the file
in.read(&contents[0], contents.size());
// ... do something ..
// Close
in.close();
}
else
throw(errno);
}
In order to detect if its ANSI or UTF-8 file, do I need to read first three bytes to check if BOM matches, or, is there a better C++11 way using codecvt? How can I make this codecvt way work for entire file?

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.
Here is some code that does what you want. Note that this code reads the input file a line at a time rather than reading it all in one fell swoop.
#include <string>
#include <fstream>
void swap_bytes (char16_t *s)
{
while (*s)
{
unsigned char *uc = (unsigned char *) s;
unsigned char swap = *uc;
*uc = uc [1];
uc [1] = swap;
++s;
}
}
int main ()
{
std::basic_ifstream <char16_t> file ("/RANDOME/PATH/file.html", std::ios::in);
if (!file)
return 1;
std::basic_string <char16_t> line;
while (std::getline (file, line))
{
swap_bytes (line.data ());
// ...
}
file.close();
}
If anything is unclear please say so in the comments.
Live demo

How to find the file pointer position in c++?

I am currently working with files in c++ and I want to read a file after a certain position. I read online that you can't open a file to read and write simultaneously. Is there a way to return the position of the file pointer at a certain moment and use it to extract the information after it?
What you read was wrong.
#include <fstream>
#include <string>
int main()
{
std::fstream file("test.txt", std::ios::in | std::ios::out | std::ios::binary);
std::string s;
file >> s;
// get current read position
auto read_pos = file.tellg();
// set current write position
file.seekp(read_pos, std::ios::beg);
static const char data[] = "aaa";
// write some data
file.write(data, 3);
}

Read a binary file into a std::vector<uint16_t> instead of std::vector<char>

I want to read a binary file containing uint16_t values. What I've done so far is:
std::ifstream is;
std::vector<char> rawfilebuffer; /* should be std::vector<uint16_t> */
is.open("uint16_t_file.raw", std::ios::binary);
is.seekg(0, std::ios::end);
size_t filesize=is.tellg();
is.seekg(0, std::ios::beg);
rawfilebuffer.reserve(filesize);
rawfilebuffer.assign(std::istreambuf_iterator<char>(is),
std::istreambuf_iterator<char>());
Using std::istreambuf_iterator<char>does not work (error: no matching conversion for functional-style cast from 'std::ifstream').
Is it possible to cast istreambuf_iterator to uint16_t?
With c++11, you can use the data() member of std::vector and read all of file (or big chunks, if the file is too big).
Something like
#include <vector>
#include <fstream>
#include <iostream>
using myType = uint16_t;
int main ()
{
std::ifstream is;
std::vector<myType> rawfilebuffer;
is.open("uint16_t_file.raw", std::ios::binary);
is.seekg(0, std::ios::end);
size_t filesize=is.tellg();
is.seekg(0, std::ios::beg);
rawfilebuffer.resize(filesize/sizeof(myType));
is.read((char *)rawfilebuffer.data(), filesize);
for ( auto const & ui : rawfilebuffer )
std::cout << '[' << ui << ']';
std::cout << '\n';
return 0;
}
Attention to the file size. If it's an exact multiple of sizeof(myType), well.
Otherwise, you should modify you resize instruction in this way
rawfilebuffer.resize(filesize/sizeof(myType)+(filesize%sizeof(myType)?1U:0U));

file manipulation in c++

I convert this code in python to c++:
content = file(filename, "rb").read()
this is the code in c++:
ifstream file;
file.open(filename, fstream::binary);
file.seekg (0, ios::end);
long fileLength = file.tellg();
file.seekg(0, ios_base::beg);
char *content = new char[fileLength];
file.read(content, fileLength);
when I run the python code I get a long string in the content (500 characters~) while the c++ code return only 4 characters.
any suggestion?
thanks
The simplest way to read an entire file is:
std::string content(
std::istreambuf_iterator<char>(std::ifstream(filename, std::fstream::binary).rdbuf()),
std::istreambuf_iterator<char>());