段故障 -> 在标准::basic_ostream<字符中,标准::char_traits<char>
segment fault -> in std::basic_ostream<char, std::char_traits<char>
我在执行时遇到段错误。下面是代码和带有 bt 的 gdb 输出。代码在崩溃之前一直运行到最后。冲突似乎与我的开关/机箱有关
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] = ' ';
//push everything into vector
if (buydbuff[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 输出:在此处输入链接说明
填充向量的代码并不能保证会有 7 个条目,但打印它们的代码假设有。