I'm working with visual studio 2013. I want to output a vector of objects into several files. I am able to create the output file if I just print everything to a single file, but if I try to output to multiple files, nothing happens.
#include<vector>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
struct object
{
int a, b;
};
int main()
{
vector<object> DATA;
//fill DATA
ofstream out; string outname;
outname = "TL" + ".txt";
out.open(outname.c_str());
for (int i = 0; i < p; i++)
{
for (int k = 0; k < DATA.size(); k++)
{
out << i << endl;
if (DATA[k].a == i)
out << DATA[k].b << endl;
}
out << endl;
}
out.close();
return 0;
}
The above works exactly as I expect. However, if I rearrange it so that I could make separate files:
for (int i = 0; i < p; i++)
{
ofstream out; string outname;
outname = "TLR" + to_string(i) + ".txt";
out.open(outname.c_str());
for (int k = 0; k < DATA.size(); k++)
{
if (DATA[k].a == i)
out << DATA[k].b << endl;
}
out.close();
}
I get no output. I already checked to see if the files were being created in another directory and nada. Placing "cout << out.is_open()" after each of the cases shows that the single file is actually being opened (output 1), while the multiple files are not being opened (output 0).
Could anyone tell me what's going on and what can I do to fix this? I don't want to have to run the program and then open the output file to parse after I've made it.
Thank you.