I am trying to make a function which takes an array 'inputLayer[]' and stores each element in the array in individual files. The first time the function is called it should create the files and the following times it is called it should append the new elements in inputLayer to the end of the file.
If I was repeatedly storing a single variable in a single file I would use something like this:
fstream recordFile;
recordFile.open ("inputRecord.txt", fstream::in | fstream::out | fstream::app);
recordFile << inputLayer[0] << endl;
In the past that has worked for me but now I have many variables which I'd like to write to individual files named "input_0_record.txt", "input_1_record.txt", ect.
In the code below I use stringstream to create the file names and then I use the same method as above to write the variables in their files.
int recordInputVariables(double inputLayer[])
{
for(int i = 0; i < inputLayerSize; i ++)
{
stringstream ss;
ss << i;
string inputNumberString = ss.str();
string recordFileName = "input_";
recordFileName.append(inputNumberString);
recordFileName.append("_record.txt");
fstream inputRecordFile( recordFileName.c_str() );
inputRecordFile.open (recordFileName, fstream::in | fstream::out | fstream::app);
inputRecordFile << inputLayer[i] << endl;
inputRecordFile.close();
}
return 0;
}
However, when I run this the file is created and the variable is written to the file the first time the function is called, but the subsequent times the function is called there are no new variables written to the files.
I'm almost sure that it is a problem with the way I'm opening the file. Can anybody spot what I've done wrong?