知道分割故障在哪里比较两个文件

Knowing where is the segmentation fault happening comparing two files

本文关键字:两个 文件 比较 分割 故障 在哪里      更新时间:2023-10-16

我有以下结构:

int main(int argc, char **argv) {
     try {
        FX3USBConnection fx3USB3Connection = FX3USB3Connection();
        fx3USB3Connection.send_text_file();
    }
    catch (ErrorOpeningLib& e) {
        printf("Error opening libraryn");
        return -1;
    }
    catch (NoDeviceFound& e) {
        printf("No device foundn");
        return 0;
    }
    return 0;
}

在send_text_files中,我要做的最后一件事是比较两个txt文件:

printf("Loopback recieved, checking if I received the same that I sendedn");
files_match(out_text_filename, in_text_filename);
printf("Exited without problem");
return; // (actually implicit)

我已经使用了2个版本的files_match函数,但最后一个是该文件的确切副本比较两个文件

bool FX3USB3Connection::files_match(const std::string &p1, const std::string &p2) {
    bool files_match;
    std::ifstream f1(p1, std::ifstream::binary|std::ifstream::ate);
    std::ifstream f2(p2, std::ifstream::binary|std::ifstream::ate);
    if (f1.fail() || f2.fail()) {
        return false; //file problem
    }
    if (f1.tellg() != f2.tellg()) {
        return false; //size mismatch
    }
    //seek back to beginning and use std::equal to compare contents
    f1.seekg(0, std::ifstream::beg);
    f2.seekg(0, std::ifstream::beg);
    files_match = std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
                      std::istreambuf_iterator<char>(),
                      std::istreambuf_iterator<char>(f2.rdbuf()));
    f1.close();
    f2.close();
    if (files_match) { printf("Files matchn"); }
    else { printf("Files not equaln"); }
    return files_match;
}

有时我会遇到错误,有时我没有。当我发现错误时,我会得到:

Loopback recieved, checking if I received the same that I sended
Files match
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

因此,调用files_match后的打印未打印,所以我想问题在功能中。但是,我在返回语句之前进行打印,并且它正确打印。

ps:我评论了该功能files_match,我没有问题。

ps1:这些文件可以具有这样的字符:¥

是的,正如@john所建议的那样,我必须添加fflush()函数。在那里,我意识到错误实际上已经超出了所有循环之外,但实际上是在离开尝试{}部分时。它对我的接触是无法销毁FX3USBConnection。

谢谢!我现在知道Fprint实际上被缓冲了。