How to append file in c++ using fstream? [duplicate]

梦想的初衷 提交于 2021-01-23 03:04:30

问题


I try to append file in C++. At start file doesn't exists. After operations there is only one line in file instead of five (5 calls of this method). It looks like file is creating, next every write operation file is cleaned out and new string is added.

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

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

I tried with fstream::in ,fstream::out. How to append string correctly in this file?

Thank you in advance.

edit:

Here is wider point of view:

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);
}

回答1:


This works.. 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;
}



回答2:


It looks like this question has been answered over yonder.

Give this a shot:

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

Edit:

I wrote this code and compiled it in Visual Studio 2012 on Windows 7 x64. It works perfectly for me. It looks like the other answer worked for you, but please let me know if this does as well.

#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;
}


来源:https://stackoverflow.com/questions/23615975/how-to-append-file-in-c-using-fstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!