2013-08-22 43 views
0

我从一个文件中提取数据(theta.txt'),使用ifstreamC++ - 分割故障关闭时ifstream的

std::ifstream theta_extracter("theta.txt", std::ifstream::in); 
std::string line; 
std::vector <double> thetas; 

    /* For each line in file until EOF */ 
while (true){ 
    getline(theta_extracter, line); /* Get line */ 

    if (theta_extracter.eof()) break; /* Stop at EOF*/ 

    std::stringstream stream(line); /* Parse Line */ 
    std::string exported; 

    /*Parse line*/ 
    while (true){ 
     stream >> exported; 
     thetas.push_back(atof(exported.c_str())); /* Store value*/ 
     if (stream.eof()) break;  
    } 
} 

theta_extracter.close(); 

如果我不关闭theta_extracter,我收到一个巨大的错误消息:

*** glibc detected *** ./combi: free(): invalid next size (fast): 0x0000000000b69ba0 *** 
======= Backtrace: ========= 
/lib64/libc.so.6[0x36c98760e6] 
/lib64/libc.so.6[0x36c9878c13] 
./combi[0x405964] 
./combi[0x4042ca] 
./combi[0x402e95] 
./combi[0x40266f] 
./combi[0x401e19] 
./combi[0x401471] 
/lib64/libc.so.6(__libc_start_main+0xfd)[0x36c981ecdd] 
./combi[0x4012c9] 
======= Memory map: ======== 
00400000-0040d000 r-xp 00000000 00:2a 59351995       /nfs/stak/students/m/moissinb/WORKSPACE/EXP3/VCG Solver/combi 
0060c000-0060d000 rw-p 0000c000 00:2a 59351995       /nfs/stak/students/m/moissinb/WORKSPACE/EXP3/VCG Solver/combi 
00b5f000-00c4a000 rw-p 00000000 00:00 0         [heap] 
36c9400000-36c9420000 r-xp 00000000 fd:00 2613       /lib64/ld-2.12.so 
36c961f000-36c9620000 r--p 0001f000 fd:00 2613       /lib64/ld-2.12.so 
36c9620000-36c9621000 rw-p 00020000 fd:00 2613       /lib64/ld-2.12.so 
36c9621000-36c9622000 rw-p 00000000 00:00 0 
36c9800000-36c998a000 r-xp 00000000 fd:00 5271       /lib64/libc-2.12.so 
36c998a000-36c9b89000 ---p 0018a000 fd:00 5271       /lib64/libc-2.12.so 
36c9b89000-36c9b8d000 r--p 00189000 fd:00 5271       /lib64/libc-2.12.so 
36c9b8d000-36c9b8e000 rw-p 0018d000 fd:00 5271       /lib64/libc-2.12.so 
36c9b8e000-36c9b93000 rw-p 00000000 00:00 0 
36c9c00000-36c9c83000 r-xp 00000000 fd:00 5272       /lib64/libm-2.12.so 
36c9c83000-36c9e82000 ---p 00083000 fd:00 5272       /lib64/libm-2.12.so 
36c9e82000-36c9e83000 r--p 00082000 fd:00 5272       /lib64/libm-2.12.so 
36c9e83000-36c9e84000 rw-p 00083000 fd:00 5272       /lib64/libm-2.12.so 
36cfc00000-36cfc16000 r-xp 00000000 fd:00 5275       /lib64/libgcc_s-4.4.7-20120601.so.1 
36cfc16000-36cfe15000 ---p 00016000 fd:00 5275       /lib64/libgcc_s-4.4.7-20120601.so.1 
36cfe15000-36cfe16000 rw-p 00015000 fd:00 5275       /lib64/libgcc_s-4.4.7-20120601.so.1 
36d0400000-36d04e8000 r-xp 00000000 fd:03 548265       /usr/lib64/libstdc++.so.6.0.13 
36d04e8000-36d06e8000 ---p 000e8000 fd:03 548265       /usr/lib64/libstdc++.so.6.0.13 
36d06e8000-36d06ef000 r--p 000e8000 fd:03 548265       /usr/lib64/libstdc++.so.6.0.13 
36d06ef000-36d06f1000 rw-p 000ef000 fd:03 548265       /usr/lib64/libstdc++.so.6.0.13 
36d06f1000-36d0706000 rw-p 00000000 00:00 0 
7fc067c6a000-7fc067c6f000 rw-p 00000000 00:00 0 
7fc067c96000-7fc067c99000 rw-p 00000000 00:00 0 
7fff52f65000-7fff52f7a000 rw-p 00000000 00:00 0       [stack] 
7fff52fff000-7fff53000000 r-xp 00000000 00:00 0       [vdso] 
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0     [vsyscall] 
Abort (core dumped) 

如果我关闭它,它赛格在这一点上过失。

在什么地方出了错任何想法?

+0

你有没有想过问题是什么? – phonetagger

+0

我仍在努力,但我还有其他需要完成的任务,然后才能再次查看。我会发布一次,我有它的工作! –

回答

0
while (getline(theta_extracter, line)){ 
..... 
while (stream >> exported){ 
      thetas.push_back(atof(exported.c_str())); /* Store value*/ 
     } 
} 

我想你应该这样做!

+0

我没有问题从文件中提取数据。我可以打印thetas并获得准确的预期结果。问题在于,当我关闭theta_extracter或只是终止函数。 –

+0

此外,这种方法也会提取EOF,因此每行的最后一个元素都会出现两次。 –

0

我不假装这是任何形式的答案。我只是发布我自己的结果&要求你检查一下你自己的....

我假设你有其他代码,你没有显示。我想知道你的其他代码是否是问题的根源。也许它会破坏内存中的其他东西,也许是堆栈?无论如何,这是我的整个测试(减去必需的#includes),它运行时不会在我的Linux x86盒子上崩溃。这段代码在你的系统上做了什么?

int main() { 

    std::cout << "Beginning..." << std::endl; 
    std::ifstream theta_extracter("theta.txt", std::ifstream::in); 
    std::string line; 
    std::vector <double> thetas; 

    /* For each line in file until EOF */ 
    while (true){ 
     getline(theta_extracter, line); /* Get line */ 
    std::cout << "\nLine: \"" << line << "\"" << std::endl; 

     if (theta_extracter.eof()) break; /* Stop at EOF*/ 

     std::stringstream stream(line); /* Parse Line */ 
     std::string exported; 

     /*Parse line*/ 
     while (true){ 
      stream >> exported; 
      std::cout << "exported = \"" << exported << "\"" << std::endl; 
      thetas.push_back(atof(exported.c_str())); /* Store value*/ 
      if (stream.eof()) break; 
     } 
    } 

// I get the same results whether I close theta_extractor or not.... 
// theta_extracter.close(); 

    std::vector<double>::iterator thit = thetas.begin(); 
    while (thit != thetas.end()) { 
     std::cout << ' ' << *thit; 
     ++thit; 
    } 
    std::cout << "\n\n...done.\n"; 
} 
+0

@ Bibi541 - 顺便说一句,尽管只要在.txt文件的最后一行有一个换行符,你的代码看起来工作正常,按照你拥有它们的方式构建你的“在EOF停止”行是很奇怪的。我会做'while(!theta_extractor.eof())'而不是'while(true)',然后放弃'if(...)break'''。与其他“while(true)”类似的问题。 – phonetagger

0

你似乎是通过写入外部缓冲区来损坏内存。它不在你显示的代码中。你一定是省略了一些东西。