Create / manipulate file within a directory other than main.cpp

1

This works, creates a file and places what I want inside it:

#include <fstream>
#include <vector>

using namespace std;
int main() {
    ofstream arquivo;
    arquivo.open("arquivo.txt");
    vector<string> v;
    v.push_back("linha1");
    v.push_back("linha2");
    v.push_back("linha3");
    if(arquivo.is_open()){
        for(auto n: v) { arquivo << n << endl; }
    }
}

But this does not work, if I want to create a new file inside a folder called "folder", it does not give error, it just does not create the file. It does absolutely nothing:

#include <fstream>
#include <vector>

using namespace std;
int main() {
    ofstream arquivo;
    arquivo.open("pasta/arquivo.txt");
    vector<string> v;
    v.push_back("linha1");
    v.push_back("linha2");
    v.push_back("linha3");
    if(arquivo.is_open()){
        for(auto n: v) { arquivo << n << endl; }
    }
}
    
asked by anonymous 20.05.2018 / 17:33

1 answer

2

The error is time to open an nonexistent file for recording. Calling the ofstream::open() method creates a new file if it does not exist, however,

20.05.2018 / 18:49