【问题标题】:How to append file in c++ using fstream? [duplicate]如何使用 fstream 在 C++ 中追加文件? [复制]
【发布时间】:2014-05-12 18:14:34
【问题描述】:

我尝试在 C++ 中附加文件。在开始文件不存在。操作后,文件中只有一行而不是五行(此方法的 5 次调用)。看起来文件正在创建,接下来每个写操作文件都被清除并添加新字符串。

void storeUIDL(char *uidl) {
        fstream uidlFile(uidlFilename, fstream::app | fstream::ate);

        if (uidlFile.is_open()) {
        uidlFile << uidl;
        uidlFile.close();
    } else {
        cout << "Cannot open file";
    }
}

我试过fstream::in ,fstream::out。如何在此文件中正确附加字符串?

提前谢谢你。

编辑:

这是更广泛的观点:

for (int i = 0; i < items; i++) {
    MailInfo info = mails[i];
    cout << "Downloading UIDL for email " << info.index << endl;

    char *uidl = new char[100];
    memset(uidl, 0, 100);
    uidl = servicePOP3.UIDL(info.index);
    if (uidl != NULL) {
        if (existsUIDL(uidl) == false) {
            cout << "Downloading mail with index " << info.index << endl;
            char *content = servicePOP3.RETR(info);

            /// save mail to file
            string filename = string("mail_" + string(uidl) + ".eml");
            saveBufferToFile(content, filename.c_str());
            storeUIDL(uidl);
            sleep(1);
        } else {
            cout << "Mail already exists." << endl;
        }
    } else {
        cout << "UIDL for email " << info.index << " does not exists";
    }

    memset(uidl, 0, 100);
    sleep(1);
}

【问题讨论】:

  • 尝试删除fstream::ate
  • 试试这个:fstream uidlFile; uidlFile.open(uidlFilename, fstream::in | fstream::out | fstream::app);
  • 这个文件中仍然只存储最后一行。
  • 是的。这很奇怪,但不起作用……请查看已编辑的问题。添加代码。

标签: c++ fstream


【解决方案1】:

这行得通..std::fstream::in | std::fstream::out | std::fstream::app .

#include <fstream>
#include <iostream>
using namespace std;

int main(void)
{

    char filename[ ] = "Text1.txt";

     fstream uidlFile(filename, std::fstream::in | std::fstream::out | std::fstream::app);


      if (uidlFile.is_open()) 
      {
        uidlFile << filename<<"\n---\n";
        uidlFile.close();
      } 
      else 
      {
        cout << "Cannot open file";
      }




   return 0;
}

【讨论】:

  • 当我尝试在循环中调用它时,只有一行带有“Text1.txt”,第二行带有“---”。和我的情况一样。我不能在一个循环中添加超过一行。只添加最后一个。
  • 好的,这就完成了。 fstream uidlFile(文件名, std::fstream::in | std::fstream::out | std::fstream::app);
【解决方案2】:

这个问题似乎已经回答了over yonder

试一试:

fstream uidFile(uidFilename, fstream::out | fstream:: app | fstream::ate);

编辑:

我编写了这段代码,并在 Windows 7 x64 上的 Visual Studio 2012 中编译了它。它非常适合我。看起来其他答案对您有用,但请让我知道这是否也适用。

#include <iostream>
#include <fstream>

using namespace std;

void save(char * string)
{
    fstream myFile("test.txt", fstream::out | fstream::app);

    if(myFile.is_open())
    {
        myFile.write(string, 100);
        myFile << "\n";
    }
    else
    {
        cout << "Error writing to file";
    }
}

int main()
{
    char string[100] = {};



    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 100; j++)
        {
            string[j] = i + 48; //48 is the ASCII value for zero
        }

        save(string);
    }

    cin >> string[0]; //Pause

    return 0;
}

【讨论】:

  • 没有任何改变。我在第一篇文章中添加了更多代码。
  • 您的文件是在一行中包含所有 5 个字符串还是仅在一行中包含第 5 个字符串?
  • 实际上在这个for循环文件之后只包含了一行,它被添加为最后一行。应该有五行文字。
  • 好吧,只是猜测...尝试添加uidFile.flush() 另外,尝试删除fstream::ate
  • 使用flush 并删除ate 后没有任何变化。
猜你喜欢
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
相关资源
最近更新 更多