C++ ifstream 有奇怪的段错误,具体取决于文件在磁盘上的位置
我有一些我继承的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有人使用固定大小的缓冲区来存储文件名。查看解析 argv 的代码,或记录文件名的代码。
Someone's using a fixed size buffer for filenames. Look at the code parsing argv, or code that logs the filename.