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

标签 c++ segmentation-fault 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。

如果没有实际的代码行,这可能很难提供帮助,我深表歉意,但我认为我无法将代码最小化为能够说明它的小型工作版本。有没有人遇到过这种事情?

最佳答案

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

关于C++ ifstream 有奇怪的段错误,具体取决于文件在磁盘上的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021505/

相关文章:

c++ - 如何将 C++ 代码集成到工作的 LAPACK 代码中

c++ - 友元函数是否违反封装?

c语言strchr段错误字符替换

c++ - 包含在 main 方法中抛出错误的类对象的 vector

ruby-on-rails - 运行配置文件测试时出现 Ruby 段错误

Android Studio 模拟器崩溃 : Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

c++ - (C++) noob - 我的代码有什么问题?

c++ - 一个生产者,两个消费者作用于生产者产生的一个 'queue'

C++ 调用显式模板构造函数

c++ - 了解多态设计 (C++)