【问题标题】:Appending data to csv file, before last entry instead of after last entry. C++在最后一个条目之前而不是在最后一个条目之后将数据附加到 csv 文件。 C++
【发布时间】:2017-10-29 02:44:23
【问题描述】:

提出这个问题并不容易,所以对于那里的任何悲伤,我深表歉意.. 我现在正在写入这样的 csv 文件:

double indicators::SMACurrentWrite() {

if ( !boost::filesystem::exists( "./CalculatedOutput/SMAcurrent.csv" ) ) // std::cout << "Can't find my file!" << std::endl;
    {
        std::ofstream SMAfile;              
        SMAfile.open("./CalculatedOutput/SMAcurrent.csv");
        SMAfile << "SMA" << endl << SMA[0] << endl; // .. or with '\n' at the end.
        SMAfile.close();
    }
else    {   
        std::ofstream SMAfile;
        SMAfile.open ("./CalculatedOutput/SMAcurrent.csv", ios::app); // Append mode
    SMAfile << SMA[0] << endl; // Writing data to file
SMAfile.close();
}
return 0;
}

应用程序每次运行时,都会在最后的输出文件中附加一个新值:

SMA
32.325

我想没有办法只在标题下(和数字上方)挤压那个新的向量条目,但无论如何这就是我想要完成的。 所以我想我必须重新读取现有的输出文件,将其放入向量中,然后替换旧文件?我是这样开始的:

double indicators::SMACurrentWrite() {

if ( !boost::filesystem::exists( "./CalculatedOutput/SMAcurrent.csv" ) ) // std::cout << "Can't find my file!" << std::endl;
    {
        std::ofstream SMAfile;              
        SMAfile.open("./CalculatedOutput/SMAcurrent.csv", ios::app);
        SMAfile << "SMA" << endl << SMA[0] << endl; // .. or with '\n' at the end.
        SMAfile.close();
    }
else    {   
        std::ofstream SMARfile("./CalculatedOutput/SMAReplacecurrent.csv");
        std::ifstream SMAfile("./CalculatedOutput/SMAcurrent.csv");

            SMARfile << SMA[0] << endl; // Writing data to file
        SMARfile << SMAfile.rdbuf();

        SMAfile.close();
        SMARfile.close();
        std::remove("./CalculatedOutput/SMAcurrent.csv");
            std::rename("./CalculatedOutput/SMAReplacecurrent.csv","./CalculatedOutput/SMAcurrent.csv");
}
return 0;
}

....,当然,这只是将新数据放在标题上方,如下所示:

32.247
SMA
32.325

..而不是这个

SMA
32.247
32.325

我希望这不会成为一项耗时的练习,但我很感激任何关于如何完成这项工作的帮助。

【问题讨论】:

  • 您不确定什么:1) 从现有文件中读取标题行并将其写入新文件,2) 将新行写入文件,以及 3) 读取其余部分原始文件并将其写入新文件。看起来你已经完成了所有这些,已经完成了 95% 的工作。在SMARfile &lt;&lt; SMA[0] &lt;&lt; endl; 之前,使用std::getline 从输入文件中读取标题行,将其写入输出文件,然后......你就完成了。结束。
  • 为什么不每次都从头开始重新创建文件?将旧内容读入内存,根据需要修改内存中的副本,将其全部写入新文件,将新文件重命名(无需删除)为旧文件。
  • 是的,确实做了很多工作。谢谢你的反馈。我不得不把它归结为睡眠不足......

标签: c++ csv ifstream ofstream


【解决方案1】:

如果您从输入文件中读取第一行,则可以使用它来启动新文件,它将文件指针留在旧数据开始的第二行。然后你可以像这样写新的东西:

if(!boost::filesystem::exists("./CalculatedOutput/SMAcurrent.csv"))
{
    std::ofstream SMAfile;
    SMAfile.open("./CalculatedOutput/SMAcurrent.csv", ios::app);
    SMAfile << "SMA" << '\n' << SMA[0] << '\n';
    SMAfile.close();
}
else
{
    std::ofstream SMARfile("./CalculatedOutput/SMAReplacecurrent.csv");
    std::ifstream SMAfile("./CalculatedOutput/SMAcurrent.csv");

    // first read header from input file

    std::string header;
    std::getline(SMAfile, header);

    // Next write out the header followed by the new data
    // then everything else

    SMARfile << header << '\n';  // Writing header
    SMARfile << SMA[0] << '\n';  // Write new data after header
    SMARfile << SMAfile.rdbuf(); // Write rest of data

    SMAfile.close();
    SMARfile.close();
    std::remove("./CalculatedOutput/SMAcurrent.csv");
    std::rename("./CalculatedOutput/SMAReplacecurrent.csv",
        "./CalculatedOutput/SMAcurrent.csv");
}

【讨论】:

  • 我将您的答案标记为有用,但由于它与 Sam Varshavchik 上面评论的过程完全相同,我不确定我是否应该将其标记为答案......无论如何,谢谢,我使用了 getline这么多次,然而....哈哈:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2012-08-12
  • 1970-01-01
相关资源
最近更新 更多