2014-11-23 67 views
-3

我在执行时遇到段错误。下面是代码和bt的gdb输出。 代码在崩溃之前一直运行到结束。 冲突似乎与我的开关/箱段错误 - >在std :: basic_ostream中<char,std :: char_traits <char>

char *getmybuyData() 
{ 
     FILE *fp = popen("php orders.php 155", "r"); 
     if (fp == NULL) perror ("Error opening file"); 
     char buydbuff[BUFSIZ]; 
     bool more = true; 
     do { 
       vector<string> vrecords; 
       for (int i = 0; (i < 7) && (more = (fgets(buydbuff, BUFSIZ, fp) != NULL)); ++i) { 
         size_t n = strlen(buydbuff); 
         if (n && buydbuff[n - 1] == '\n') 
           buydbuff[n - 1] = '\0'; 
         //push everything into vector 
         if (buydbuff[0] != '\0') 
           vrecords.push_back(buydbuff); 
         //begine breaking down the data; 
       } 
       for (int n = 0; n < 7; ++n){ 
       switch(n){ 
         case 0: 
           cout << vrecords[0] << endl; 
         break; 
         case 1: 
           cout << vrecords[1] << endl; 
         break; 
         case 2: 
           cout << vrecords[2] << endl; 
         break; 
         case 3: 
           cout << vrecords[3] << endl; 
         break; 
         case 4: 
           cout << vrecords[4] << endl; 
         break; 
         case 5: 
           cout << vrecords[5] << endl; 
         break; 
         case 6: 
           cout << vrecords[6] << endl; 
         break; 
         case 7: 
           cout << vrecords[7] << endl; 
         break; 
         default: 
           //cout << "Hello World" << endl; 
         break; 
       } 
       } 

     } while (more); 
} 

输出:

198397652 
2014-11-14 15:10:10 
Buy 
0.00517290 
0.00100000 
0.00100000 
0.00000517 
198397685 
2014-11-14 15:10:13 
Buy 
0.00517290 
0.00100000 
0.00100000 
0.00000517 
198398295 
2014-11-14 15:11:14 
Buy 
0.00517290 
0.00100000 
0.00100000 
0.00000517 
203440061 
2014-11-21 16:13:13 
Sell 
0.00825550 
0.00100000 
0.00100000 
0.00000826 
Segmentation fault 

GDB输出: enter link description here

+0

函数的* line *是指向哪个堆栈跟踪(也就是第45行)? – 2014-11-23 12:49:13

+0

你的'案例7'永远不会被执行,'default'也不会被执行。 – 2014-11-23 12:50:06

+1

[在过去48小时内]发布此任务上的问题(http://stackoverflow.com/users/4272671/bro?tab=questions)将提示您可能需要对语言基础进行审查。该输出for循环有点证实了这一观察。 – WhozCraig 2014-11-23 13:05:14

回答

0

你的代码来填充向量不保证会有7条目,但你的代码打印他们假设有。

相关问题