0

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?

Hugh
  • 413
  • 1
  • 5
  • 12

2 Answers2

2
 inputRecordFile.open (recordFileName, fstream::out | fstream::app);

Don't use fstream::in in this context.

vacuumhead
  • 479
  • 1
  • 6
  • 16
  • Thank you, I solved it, in addition to this my line of code: fstream inputRecordFile( recordFileName.c_str() ); should have just been: fstream inputRecordFile; – Hugh Mar 26 '16 at 15:49
1

You can code same problem as:

int recordInputVariables(double inputLayer[])
{  
    for(int i = 0; i < 5; i ++)
    {
        stringstream ss;
        ss << i;
        string inputNumberString = ss.str();
        string recordFileName = "input_";
        recordFileName.append(inputNumberString);
        recordFileName.append("_record.txt");
        cout << recordFileName << endl;
        ofstream inputRecordFile(recordFileName.c_str());

        // Here you have opened file by using constructor(above) so don't
        // need to open it again as below
        // inputRecordFile.open (recordFileName, ofstream::out);
        inputRecordFile << inputLayer[i] << endl;
        inputRecordFile.close();
        cout << "loop" << endl;
    }
    return 0;
}

Now you run this code and everything will be as you desired...

Shiv
  • 122
  • 2
  • 16