ofstream not writing to end of existing text file in c++ -


its not appending end of created text file (which has contents in it) specify cin , when have out2.open(tablename2, std::ofstream::out | std::ofstream::app); , out2 << v2[i]; in there.

full code:

#include "stdafx.h" #include <iostream> #include <string> #include <fstream> #include <vector>   #include <iomanip> #include <stdio.h> using namespace std; using std::vector; using std::string; void insertit(std::vector<std::string>& v, std::vector<std::string>& v2, std::string insertedstr) {          std::string tablename2;         cout << "enter file name extension " << endl;         std::getline(std::cin, tablename2);         (int w = 0; w < v.size(); w++) {             cout << v[w] << ": ";             cin.ignore();             std::getline(std::cin, insertedstr);             v2.push_back(insertedstr + " ");             insertedstr.clear();         }         //below, why not writing file specified cin >>tablename2?         std::ofstream out2(tablename2);         out2.open(tablename2, std::ofstream::out | std::ofstream::app);         (int = 0; < v2.size(); i++) {             out2 << v2[i];         }         out2.close();         v2.clear();         cout << "the record has been inserted " << tablename2 << endl;     } int main() { std::vector<std::string> v = {"author", "title"}; std::vector<std::string> v2; std::string insertedstr;  insertit(v, v2, insertedstr);    return 0; } 

any ideas why?

the constructor opens file, although not in appending mode. it's not clear me (i looked @ http://en.cppreference.com/w/cpp/io/basic_filebuf/open , didn't much, , i'm not standards wizard). if constructor looks like

        std::ofstream out2(tablename2, std::ofstream::out | std::ofstream::app); 

and remove out.open(....) call, code works (tested gcc 4.8.4 - removed #include "stdafx.h").


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -