I am trying to write a program using C++ performs XOR operation between a string and a char. The result is stored in a file and read back later. Certain outputs of the XOR operation cause the characters to become control characters like SOH, ACK, STX, BEL. These control characters then stop the program from progressing so it will only write or read half the XOR operation output to the file. I tried using ios::binary mode to avoid this from happening but the problem still persists. Are there any other methods to forcefully avoid control character operation?
Asked
Active
Viewed 623 times
0
-
4The file mode is irrelevant (it only affects newline conversion, and only on Windows among the mainstream OSes). Please post a [mcve], and describe your problems in more detail. – molbdnilo Feb 21 '22 at 13:11
-
Read, write, and read back as binary. And perform your binary operations on `std::vector
`, for example. If you want to interpret your data as text after the second read, you will need a way of decoding what you first encoded (a way to revert that XOR operation). – rturrado Feb 21 '22 at 14:08
1 Answers
0
Yes, regardless of any control characters, it can be simply stored. Please use unformatted IO funcions of the stream. Please read here and here about unformatted io functions. So, for example put, write or get and read.
Opening the file in binary or text mode will basically make no difference in your application. Please read here abaout the difference of the modes.
With the above know how, the function can be implemented easily.
Please see:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
// Show string as characters and as a sequence of the related codes
void output(const std::string& title, std::string& s) {
std::cout << std::left << '\n' << title << '\n';
// Show all characters of the string
for (const char c : s)
std::cout << std::setw(4) << c;
std::cout << "\n" << std::dec;
// Show integer equivalent of all chars
for (const char c : s)
std::cout << std::setw(4) << static_cast<unsigned int>(static_cast<unsigned char>(c));
std::cout << "\n";
// Show integer equivalent of all chars
for (const char c : s)
std::cout << std::hex << std::setw(4) << static_cast<unsigned int>(static_cast<unsigned char>(c));
std::cout << "\n\n";
}
int main() {
// The test string
std::string hello{ "Hello world" };
output("Original String", hello);
// Flip all bits
for (char& c : hello) c ^= 255;
output("Xored String", hello);
// Here we will store the data
const std::string filename{ "r:\\test.bin" };
// Open file for binary output
std::ofstream ofs(filename, std::ios::binary);
// Check, if it could be opened
if (ofs) {
// Save all data
for (char& c : hello)
ofs.put(c);
ofs.close();
// Clear all data in hello
hello.clear();
output("Empty hello", hello);
// Now open file for input
std::ifstream ifs(filename, std::ios::binary);
// Check, if it could be opened
if (ifs) {
// Read all data from file
char k;
while (ifs.get(k))
hello += k;
output("hello, read back from file", hello);
// Flip all bits
for (char& c : hello) c ^= 255;
output("Xored String", hello);
}
else std::cerr << "\nError: Could not open file '" << filename << "'for input\n";
}
else std::cerr << "\nError: Could not open file '" << filename << "'for output\n";
}

A M
- 14,694
- 5
- 19
- 44