段故障 - &gt;在std :: basic_ostream <char,std :: char_traits <char =“”> </char,>

时间:2014-11-23 12:46:16

标签: c++ switch-statement

我在执行时遇到段错误。下面是代码和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] = '\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

1 个答案:

答案 0 :(得分:0)

您填写向量的代码并不能保证会有7个条目,但您打印它们的代码会假定有。