读/写文件,字节丢失,可能是字符溢出

发布于 2025-01-02 10:19:31 字数 1362 浏览 1 评论 0原文

我对编程很陌生,有时甚至我写的东西对我来说仍然很神秘。这是我第一次在互联网上寻求帮助。

我这里遇到了一个问题,并且已经有一段时间无法解决它了。

这就是我想做的:
从文件中逐字节读取到向量中,修改字节并将其写回。

这就是发生的事情:
所有的加密内容似乎都有效,但不知何故,在几次这样的操作之后,文件的一部分丢失了。

在字节修改过程中,我使用字符溢出,这意味着我向向量的每个部分添加一些随机数并将其作为一个整体写回。

在我看来,这可能是某种我不知道的算术溢出问题。

顺便说一句,我使用 Qt SDK 和 Windows 环境,以防有帮助。

这是代码片段:

void crypt(string * password,string filename, int sign){
    vector<char> stack;
    ifstream is;
    is.open(filename.c_str());
    char c;
    for (int i = 0; !is.eof(); i++){
        is >> noskipws >> c;
        stack.push_back(c);
    }
    stack.pop_back();
    is.close();
    int code = 0;
    double x;
    char method = 0;
    int var;
    for (int i = 0; i < password->size(); i++)
        code += password->at(i);
    for (int i = 0; i < (stack.size()); i++){
        // some unrelated stuff skipped
        if (sign == 1)code += stack[i];
        stack[i] += var*method*sign;    //<----this might be the cause!
        if (sign == -1)code += stack[i];
        method = 0;
    }
    ofstream os;
    os.open(filename.c_str());
    for (int i = 0; i < stack.size(); i++){
        os << noskipws << stack[i];
    }
    os.flush();
    os.close();
}

很抱歉代码中的混乱,我编写它是为了测试。

任何想法将不胜感激。

I am new to programming and sometimes even the stuff I write remains mysterious to me. This is my first time asking for help on the internet.

I have a problem here and I haven't been able to solve it for some time now.

This is what I want to do:
Read byte by byte from a file into a vector, modify the bytes and write them back.

This is what happens:
All the encryption stuff seem to work, but somehow after several of these operations a part of the file goes missing.

In the byte modifying process I use char overflows, that means I add some random number to each part of my vector and write it back as a whole.

In my opinion this could be some kind of arithmetic overflow issue I don't know about.

By the way, I use Qt SDK and Windows environment, in case that helps.

Here's the code fragment:

void crypt(string * password,string filename, int sign){
    vector<char> stack;
    ifstream is;
    is.open(filename.c_str());
    char c;
    for (int i = 0; !is.eof(); i++){
        is >> noskipws >> c;
        stack.push_back(c);
    }
    stack.pop_back();
    is.close();
    int code = 0;
    double x;
    char method = 0;
    int var;
    for (int i = 0; i < password->size(); i++)
        code += password->at(i);
    for (int i = 0; i < (stack.size()); i++){
        // some unrelated stuff skipped
        if (sign == 1)code += stack[i];
        stack[i] += var*method*sign;    //<----this might be the cause!
        if (sign == -1)code += stack[i];
        method = 0;
    }
    ofstream os;
    os.open(filename.c_str());
    for (int i = 0; i < stack.size(); i++){
        os << noskipws << stack[i];
    }
    os.flush();
    os.close();
}

Sorry for the mess in the code, I kind of wrote it for testing.

Any ideas will be appreciated.

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

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

发布评论

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

评论(1

长亭外,古道边 2025-01-09 10:19:31

您正在以“文本”模式打开文件,这可能会导致问题,特别是因为您的输出字符最终肯定会超出可打印 ASCII 字符的范围。这可能会导致在 Windows 上出现问题,例如,当您尝试输出值 0xD(回车符)时,库会将其转换为 0xD 后跟 0XA(换行符)。

因此,尝试以二进制模式打开文件,如下所示:

os.open(filename.c_str(), ios::binary);

You are opening your files in "text" mode, this can cause problems especially since your output characters will most certainly end up outside the range of printable ASCII characters. This can cause issues as on windows for example, when you try to output the value 0xD (carriage return), the library will convert that into a 0xD followed by 0XA (line feed).

So, try to open your files in binary mode like this:

os.open(filename.c_str(), ios::binary);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文