在 c++ 中使用 fstream读/写二进制数据和控制字符

发布于 2025-01-09 02:10:40 字数 207 浏览 0 评论 0原文

我正在尝试使用 C++ 编写一个程序,在字符串和字符之间执行 XOR 操作。结果存储在文件中并稍后读回。 XOR 运算的某些输出会导致字符变成控制字符,例如 SOH、ACK、STX、BEL。然后,这些控制字符会阻止程序继续运行,因此它只会将 XOR 操作输出的一半写入或读取到文件中。我尝试使用 ios::binary 模式来避免这种情况发生,但问题仍然存在。还有其他方法可以强制避免控制字符操作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2025-01-16 02:10:40

是的,无论任何控制字符,都可以简单存储。请使用流的未格式化IO函数。请阅读此处此处了解未格式化的 io 函数。因此,例如 put写入get读取

以二进制或文本模式打开文件基本上不会对您的应用程序产生任何影响。请阅读此处了解模式的差异。

有了上面的知识,就可以轻松实现该功能了。

请参阅:

#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";
}

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文