ofFile : .TXT not being recorded (help!)

Hi there,

I am currently doing an app (Rpi2, Jessie) where I need to, during update, send a line to be written into a TXT, and after some time, it saves the data and parses a new TXT file. I have already managed to do this, but only when I create an instance of ofFile locally within a function, as follows:

string path = ofToString(“test.txt");
ofFile myTextFile(ofToDataPath(path),ofFile::WriteOnly);
myTextFile.create();
myTextFile << “my text comes here”+”\n”;
myTextFile << “my text comes here”+”\n”;
myTextFile.close();

However, I now need to create a ‘global’ declaration of my ofFile (myTextFile), and send text to it from time to time, and only even further close it. Thus, I declare in the header:

ofFile myTextFile;

And create it during setup:

string path = ofToString(“test.txt");
myTextFile = ofFile(ofToDataPath(path),ofFile::WriteOnly);
myTextFile.create();

And it works, the file is created. But when I try to send text to it in the update with:

myTextFile << “my text comes here”+”\n”;

And after a while, I call:

myTextFile.close();

It simply won’t work. I have tried everything in this setup, but nothing seems to work. I have tested it both in a Mac and at my Rpi2, both unsuccessful.

Anyone one could please give me a light?

Thanks hugely!

ofFile::create() is the problem as it creates the file, then closes it. The file does not stay open. You should check if the file exists, create if necessary, then call open(). At this point, you can write stuff to it.

doing

ofFile myTextFile("test.txt",ofFile::WriteOnly);
myTextFile << “my text comes here”+”\n”;

will create the file if it doesn’t exist and you can write to it right away

Thanks for the reply!

I do not need to check if it exists - the files have dynamically unique names. I have tried out opening it after creating it as suggested, but nothing gets saved as well.

Thanks for the reply Arturo!

Indeed it works. However, on this way I would need to create an instance of ofFile each time I want to send a string to the text file? Plus, I have tested calling that twice in a roll, and it does not work for the second time already that it is called. The context is: I have a function that I call on update() each time I need to record to a text file a string.

you can open the file in setup and then write to it on every update like:

//.h
ofFile myTextFile;

//setup
myTextFile.open("text.txt",ofFile::WriteOnly);

//update
myTextFile << "some text"

if you need to open a file to append to it instead of overwrite it’s contents then you can do:

ofFile myTextFile("test.txt",ofFile::Append);
myTextFile << “my text comes here”+”\n”;
1 Like

Solved! Thanks hugely, Arturo.

Hi @arturo,
I am having a similar problem but I can’t solve it. I am trying to dinamically write a series of txt files that should record a stream of floats coming from a device. I managed to write 3 files, thanks to your hint here, but the files are empty… Any further suggestion, perhaps? Thank you!

here is what I have done:

in .h

ofBuffer bufferAcc[ACCEL];
ofFile fileAcc[ACCEL];

in setup

  for (int a = 0; a<ACCEL; a++) { 
        bufferAcc[a].begin();
        fileAcc[a].open(path + "/AccData_" + ofToString(a) + ".txt",ofFile::WriteOnly);
   }

in update

for (int a = 0; a<ACCEL; a++) {
    bufferAcc[a].append(ofToString(myo.getDevices()[i]->getAccel().getPtr()[a]) + "\n");
    fileAcc[a].writeFromBuffer(bufferAcc[a]);
//alternatively your version
// fileAcc[a] << ofToString(myo.getDevices()[i]->getAccel().getPtr()[a] )+ "\n";
    fileAcc[a].create();
 }

ofFile::create creates a new file overwriting the previous one if it exists, so that last call you do inside the for loop is actually removing the file you’ve already created, just remove that last line and it should work

@arturo : doh!
thanks! :slight_smile:

Hello @arturo sorry to revive this. It works perfectly but it only updates the txt when I close the application. (trigering MyTextFile << "tst" with keypress). Is this normal? How can I write and see realtime?

Thanks