C++ ifstream 有奇怪的段错误,具体取决于文件在磁盘上的位置

发布于 2024-10-17 21:09:02 字数 1652 浏览 0 评论 0原文

我有一些我继承的 C++ 代码(使用 ROOT 库)(约 1.5k 行),并且是第一个我想做的事情是解决与打开和读取在运行时作为参数传递的二进制文件有关的错误。代表该问题的一些代码如下所示:

[~/this]$ ./Make_RWQ_Tree /Users/me/this/that/mydata_f00000001.rwq
inFile: ./mydata_f00000001.rwq
outFile: ./mydata_f00000001.tru
Header Info:
a = 12
b = 345
c = 51
N = 100
G = 100
numEventsInFile = 1000
   r = 1
   s = 2
   t = 4
*** Show info for each of the events in the file normally ***

[~/this]$
[~/this]$ mv ./mydata_f00000001.rwq /Users/me/this/that/another/directory/deeper/
[~/this]$ ./Make_RWQ_Tree /Users/me/this/that/another/directory/deeper/mydata_f00000001.rwq
inFile: ./mydata_f00000001.rwq
outFile: ./mydata_f00000001.tru
Header Info:
a = 12
b = 345
c = 51
N = 100
G = 100
numEventsInFile = 1000

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

#5  0x00007fffffe007c5 in __memcpy ()
#6  0x00007fff87de59f7 in std::basic_streambuf<char, std::char_traits<char> >::xsgetn ()
#7  0x00007fff87dc7b19 in std::basic_filebuf<char, std::char_traits<char> >::xsgetn ()
#8  0x00007fff87dcd8c1 in std::istream::read ()
#9  0x0000000100001e25 in main (argc=<value temporarily unavailable, due to optimizations>, argv=<value temporarily unavailable, due to optimizations>) at Make_RWQ_Tree.cxx:175

因此,根据文件所在的位置,在成功打开文件并从文件中读取多个值后,我会收到一个段错误。报告的第 175 行只是一个常规的 inFile.read。

我很抱歉,如果没有实际的代码行,这可能很难提供帮助,但我认为我无法将代码最小化为说明它的小型工作版本。有人有发生过这种事情的经验吗?

I have some C++ code (using ROOT libraries) I've inherited (~1.5k lines) and one of the first things I'm trying to do is solve a bug having to do with opening and reading a binary file which is passed as an argument at run time. Some code to represent the problem would look like:

[~/this]$ ./Make_RWQ_Tree /Users/me/this/that/mydata_f00000001.rwq
inFile: ./mydata_f00000001.rwq
outFile: ./mydata_f00000001.tru
Header Info:
a = 12
b = 345
c = 51
N = 100
G = 100
numEventsInFile = 1000
   r = 1
   s = 2
   t = 4
*** Show info for each of the events in the file normally ***

[~/this]$
[~/this]$ mv ./mydata_f00000001.rwq /Users/me/this/that/another/directory/deeper/
[~/this]$ ./Make_RWQ_Tree /Users/me/this/that/another/directory/deeper/mydata_f00000001.rwq
inFile: ./mydata_f00000001.rwq
outFile: ./mydata_f00000001.tru
Header Info:
a = 12
b = 345
c = 51
N = 100
G = 100
numEventsInFile = 1000

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

#5  0x00007fffffe007c5 in __memcpy ()
#6  0x00007fff87de59f7 in std::basic_streambuf<char, std::char_traits<char> >::xsgetn ()
#7  0x00007fff87dc7b19 in std::basic_filebuf<char, std::char_traits<char> >::xsgetn ()
#8  0x00007fff87dcd8c1 in std::istream::read ()
#9  0x0000000100001e25 in main (argc=<value temporarily unavailable, due to optimizations>, argv=<value temporarily unavailable, due to optimizations>) at Make_RWQ_Tree.cxx:175

So depending on where the file is I get a Seg Fault after it has successfully opened and read several values out of the file. Line 175 as reported is just a regular inFile.read.

I apologize that this might be very difficult to help without actual lines of code, but I don't think I can minimize the code to a small working version that illustrates it. Anyone have any experience with this kind of thing happening?

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

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

发布评论

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

评论(1

还在原地等你 2024-10-24 21:09:02

有人使用固定大小的缓冲区来存储文件名。查看解析 argv 的代码,或记录文件名的代码。

Someone's using a fixed size buffer for filenames. Look at the code parsing argv, or code that logs the filename.

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