Question

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);
}
Was it helpful?

Solution

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

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top