How to make copies of the executable itself in C++?

I want to make copies of the exe file itself multiple times. I tried the following code: #include <fstream> #include <string> int main() { std::ifstream from("main.exe", std::ios::binary); auto buf { from.rdbuf() }; for(int x { 0 }; x <= 10; ++x) { std::string name { "main" + std::to_string(x) + ".exe" }; std::ofstream out(name, std::ios::binary); out… Read More How to make copies of the executable itself in C++?

How does an std::string field in a struct gets written to an std::fstream?

Here is an example struct: struct Person { std::string Name; int Age; }; And Here is how i write it to an fstream: Person p; p.Name = "Mike"; p.Age = 21; stream.write((char*)&p, sizeof(p)); As you can see above I write my Person variable to an fstream using write() function. Person’s name is written to the… Read More How does an std::string field in a struct gets written to an std::fstream?

I am using the ifstream function in c++ and I need to save two words as one string instead of splitting them

I need to read in a line from a file in c++ but need to save two strings as one. For example, if the the files line had someones name like Bernie Sanders, I would want to save the entire name into a string variable instead of just the first name. >Solution : I assume… Read More I am using the ifstream function in c++ and I need to save two words as one string instead of splitting them

C++ istream::read

Why this function read files wrong? In buff after read last symbols contain garbage values. Does it related with memory alignment? I know about more safe std::string and rdbuff(), but I want to understand how work C-style strings. char * read_from_file(const char * path) { std::ifstream fin(path); if (fin) { fin.seekg(0, fin.end); size_t length =… Read More C++ istream::read