与流相关的分段故障

时间:2014-06-26 22:47:35

标签: c++ io segmentation-fault fstream

我被困在这个......似乎无法在任何地方找到解决方案......

我有一个由SARdataPoints组成的班级SAR。 SAR计算与SARdataPoints中包含的股票价格相关的分析值。

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <array>
#include "SARdataPoint.h"
#include "SAR.h"

using namespace std;

int main()
{

array<string, 10> files = {"/home/reechee/Documents/Cpp/HW3/ASPS.csv",
                           "/home/reechee/Documents/Cpp/HW3/AZO.csv",
                           "/home/reechee/Documents/Cpp/HW3/CNP.csv",
                           "/home/reechee/Documents/Cpp/HW3/COST.csv",
                           "/home/reechee/Documents/Cpp/HW3/CVS.csv",
                           "/home/reechee/Documents/Cpp/HW3/DBP.csv",
                           "/home/reechee/Documents/Cpp/HW3/DGL.csv",
                           "/home/reechee/Documents/Cpp/HW3/JJP.csv",
                           "/home/reechee/Documents/Cpp/HW3/ORLY.csv",
                           "/home/reechee/Documents/Cpp/HW3/PHYS.csv"};

SAR SAR1 = SAR(); // initialize SAR object

ofstream output; // initialize output file
output.open("/home/reechee/Documents/Cpp/HW3/output.txt"); // open output file

for(auto& f : files)
{
    cout << "Now reading file: " << f << '\n';
    SAR1.parse(f);
    SAR1.initComp();
    SAR1.calcSAR();
    SAR1.printSARfile(output);
}

output.close(); // close output file
return 0;

} // end main()

void SAR::printSARfile(std::ofstream& file)
{
    file << std::setw(8) << "Date" << " ";
    file << std::setw(8) << "High" << " ";
    file << std::setw(8) << "Low" << " ";
    file << std::setw(8) << "EP" << " ";
    file << std::setw(8) << "Alpha" << " ";
    file << std::setw(8) << "Trend" << " ";
    file << std::setw(8) << "SAR"  << " " << '\n';

    for(auto const& d : data)
    {
        file <<  std::setw(8) << d.second.getDate() << "  ";
        file <<  std::setw(8) << d.second.getHigh() << "  ";
        file <<  std::setw(8) << d.second.getLow() << "  ";
        file <<  std::setw(8) << d.second.getEP() << "  ";
        file <<  std::setw(8) << d.second.getAlpha() << "  ";
        file <<  std::setw(8) << d.second.getTrend() << "  ";
        file <<  std::setw(8) << d.second.getSAR() << '\n';
    }
}

如果我注释掉printSARfile(输出),它运行时没有错误。有了它就会抛出一个seg错误。我知道计算功能运行正常,因为我可以将所有内容打印到控制台。如果我尝试使用类似

的内容写入文件
output << "TEST";

工作正常,但只有当该行直接在main()中,而不是在其中一个for循环中。它应该是一件简单的事情,我只是无知。如果我在这里和那里放入一些output.ios :: good(),那么在文件打开和关闭之间的每一个地方都是好的。

抱歉墙上的文字。 SAR :: parse()按要求包含在下面。

//The function parse() uses the Boost Tokenizer library to parse the date, high,
//and low values into SARdataPoint objects.
void SAR::parse(std::string filepath)
{
typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
std::string line;

std::ifstream file(filepath);

if (file.is_open())
{
    while (getline(file, line))
    {
       Tokenizer tok(line);

        for(Tokenizer::iterator iter = tok.begin(); iter != tok.end(); ++iter)
        {
            int currentIndexInt;
            std::string currentIndexStr;

            //something in here was duplicating the final digit of the date,
            //second call to erase is a truncation work-around
            if(std::distance(tok.begin(), iter) == 0 && *iter != "Date")
            {
                currentIndexStr = *iter;
                currentIndexStr.erase(std::remove(currentIndexStr.begin(), 
                                                  currentIndexStr.end(), '-'));
                currentIndexStr.erase(currentIndexStr.size() - 1);
                currentIndexInt = std::stoi(currentIndexStr);
                this->addDataPoint(currentIndexInt);
            }

            if(std::distance(tok.begin(), iter) == 2  && *iter != "High")
            {
                this->data[currentIndexInt].setHigh(std::stod(*iter));
            }

            if(std::distance(tok.begin(), iter) == 3 && *iter != "Low")
            {
                this->data[currentIndexInt].setLow(std::stod(*iter));
            }
        }
    }
    file.close();
}
else 
{
    std::cout << "Unable to open file"; 
}
}

这里是full git repo(抱歉延迟,之前从未使用过git)。

1 个答案:

答案 0 :(得分:0)

我最终发现,如果我在for循环中初始化ofstream对象,迭代CSV文件,那么事情就好了。我无法找到段错误的原因,但这是一个功能性的解决方法。但是,在这样做时,文件只是在每次迭代中从头开始重写,所以我不得不完全消除for循环,只需手动为每个文件专门运行代码,一次一个。