无法完整地返回向量集{}

时间:2014-11-24 03:13:04

标签: c++ vector

我的回报值v;只返回一组{}:

输出:

198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517

预期产出:

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

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>


std::vector<std::string> getmybuyData()
{
        FILE *fp = stdio;
        if (fp == NULL) perror ("Error opening file");
        char buydbuff[BUFSIZ];
        bool more = true;
        do{
                std::vector<std::string> v;
                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';
                        v.push_back(buydbuff);
                        //return v;
                }
                for(int p = 0; p < v.size(); ++p){
                        std::cout << v[p] << std::endl;
                }
        return v;
        } while (more);
}

std::vector<std::string> markData(){
        std::vector<std::string> vd;
        vd = getmybuyData();
        for (int j=0; j < vd.size(); ++j)
        {
                std::cout << vd[j] << std::endl; //main goal is to print data here.
        }

        return vd;
}
int main()
{
        markData();
}

我的代码应该从向量中收集多个记录集{},但只会返回一个集{}或段错误或两者。取决于版本。 。 std :: cout&lt;&lt; vd [j]是我尝试打印数据的地方。 如何返回所有记录而不只是一个?

注意: std :: cout&lt;&lt; v [p]&lt;&lt;的std :: ENDL; &#39;将&#39;用段错误打印出所有值&#39;如果&#39;我评论了返回v。

link gdb output

0 个答案:

没有答案