Input Text set variable

Hello community!
I come to you because I am currently developing an application using ofxUI.
I would like to know if it was possible to use an input text to increment a variable “TIP” and insert here:

ofFile file ("TIP" + "txt." OfFile :: WriteOnly);

How to achieve this?
Thank you to you for your help


If you want to save text file, just use:

ofFile file("TIP.txt",ofFile:WriteOnly);

I misspoke. I would save my file with the name corresponding to the content of my text input field.
For example if the user enters “text1” in the field and make enter, I will appoint texte1.txt file.
TIP was an example of variable.
Is this clearer?

If TIP is a string variable, then you can already use it to refer to the string you want for the file name. There is no need for quotation marks, in fact the quotation marks make it “just a string” (aka the word “TIP”) instead of the variable pointing to the actual string. So the solution is to remove the quotation marks.


string TIP = "text1";
ofFile newFile(ofToDataPath(TIP + ".txt"), ofFile::WriteOnly);

I understand for the quotation marks. But my problem is even different. I would like the TIP variable is incremented by a text field filled by the user. In fact, the user opens my app and input text asks him to enter his name. This name will then be the title of the txt document. The code change is this:

else if(e.getName() == "TEXT INPUT")
        ofxUITextInput *textinput = (ofxUITextInput *) e.widget;
       then **TIP=ENTERED TEXT**

Ok, so your problem is to get the text field input into a string. The best place to do that is in the guiEvent method. Below is an example that shows you how to set this up. You have a text field with a certain name. Then in the guiEvent method, you check this name. Then you get the input string from the text input and store it. In this example the name is used to create a text file, as was your original question. Of course you could also declare this filename string in a broader scope (for example your app’s .h file), so you can access it in other place of your app as well. In that case you could merely set the filename string in the guiEvent method, using the methods shown below.

// add a GUI element called "TIP" in the setup method
gui->addTextInput("TIP", "", OFX_UI_FONT_SMALL);

// add something like the following in the guiEvent method
void ofApp::guiEvent(ofxUIEventArgs &e) {
    string name = e.widget->getName();
    if(name == "TIP") {
            ofxUITextInput *textInput = (ofxUITextInput *) e.widget;
            string filename = textInput->getTextString();
            if (filename.length() > 0) {
                    ofFile newFile(ofToDataPath(filename + ".txt"), ofFile::WriteOnly);

Thank you for your help. I should not be far away but there are a concern. When I write a text, a window shows me this: Run-Time Check Failure #3 - The variable ‘filename’ is being used without being initialized.

Here is my code in App.h:

void save() {
	int frameNr = ofGetFrameNum();
	int filename;    
			ofFile file(filename + ".txt",ofFile::WriteOnly); 
	for (int i = 0; i < maxElements; i++)
		file <<  ofToString(yValues[i],2) + "\n"; 
	ofLogNotice("Fichier enregistre");

and here is my code in App.cpp:

	else if(e.getName() == "SAUVEGARDER")
    ofxUILabelButton *button = (ofxUILabelButton *) e.widget;;             
else if(e.getName() == "NOM") {
        ofxUITextInput *textInput = (ofxUITextInput *) e.widget;
        string filename = textInput->getTextString();
        if (filename.length() > 0) {
                ofFile newFile(ofToDataPath(filename + ".txt"), ofFile::WriteOnly);

Where is my mistake ?
Thanks a lot !

The problem is that the code in your save() method is incorrect. You have an int variable called filename, that you don’t initialize and then use. Hence the error. From the code, I assume you want to save a text file each frame and have it consist of certain values. You probably want to change “int filename” to the following:

string filename = ofToString(frameNr);

Also, actual methods should be defined in the .cpp file. Only the method (name) itself should be declared in the .h file. See all the examples that come with OpenFrameworks.

Thanks a lot, it works !