How can I write to a text file using a GUI in c++? - user-interface

The code I have does not write to a text file even after establishing a text file in the code and using several write methods.
I am using a window GUI maker. The GUI consists of four buttons that are change a label. The problem occurring is that after closing the GUI, the value of the button that was last pressed is supposed to be saved into a text file, however, the code does not save it. Here is an example of on of the buttons.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//label3->Text = "Winter";
//vector<String> stringList;
//stringList.push_back("");
fstream stringFile;
sudden = "Winter";
label3->Text = sudden;
stringList.push_back("Winter");
stringFile.open("lastButton.txt", fstream::app);
for (int i = 0; i < stringList.size(); i++) {
stringFile << "Winter. /n";
}
stringFile.close();
}

Related

FLTK window not showing in while loop c++

I am trying to implement a game loop in FLTK
void SnakeFLTK::init() {
_display = new Fl_Window(900, 600);
if (!_display)
throw SnakeFLTKException("Couldn't make fltk window!");
_display->color(FL_BLACK);
_display->show();
while (!_doExit) {
std::cout << "-->" << std::endl;
}
Fl::run();
}
the problem I have is the window is not showing. I want to keep showing and redrawing on the window in the while (!_doExit) loop and it's important that I use _doExit. I have tried using
while (Fl::wait > 0)
but this method seems to have its own loop that waits for events.
How do I Implement a loop like I did and show the window?
FLTK is doing nothing until Fl::run is called. And as this, you can not do anything after you call Fl::run because the function returns only if main window is closed.
Exactly for doing something while Fltk itself is "running" you can register to the idle loop like this:
void CallbackFunc( void* )
{
std::cout << "Hallo" << std::endl;
}
int main() {
auto _display = new Fl_Window(900, 600);
_display->color(FL_BLACK);
_display->show();
Fl::add_idle( CallbackFunc );
Fl::run();
}
In the given callback function you can do the drawing or anything youl like to achieve in FLTK which is not driven by events coming from the active widgets itself.

Clear Firemonkey TListView search text (C++)

I want to clear the ListView's search box text in C++. Here is a link to doing this in pascal but I've been unable to convert this to C++ (due to my ignorance).
Seems like it should be something simple like:
ListView1->SearchBox->Clear();
thanks, russ
#include <FMX.SearchBox.hpp>
TSearchBox *SearchBox_ListView1 = nullptr;
...
for(int i = 0; i < ListView1->Controls->Count; ++i)
{
SearchBox_ListView1 = dynamic_cast<TSearchBox*>(ListView1->Controls[i]);
if (SearchBox_ListView1)
break;
}
...
if (SearchBox_ListView1)
SearchBox_ListView1->Text = L"";

How to put setText() in JTextArea with for loop?

So I am making this program(WindowBuilder). The user puts a word in the textField and in the other textField how many times he wants it to be repeated in the JTextArea.
private class BtnRepeterActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String texte = textField.getText();
for (int i = Integer.parseInt(textField2.getText()); i >0; i++){
texte = texte + "\n";
}
textArea.setText(texte);
}
This code makes the program freeze whenever I click on my button. I tried putting "return" but that puts an error. I do not know what to do next.
EDIT**: Putting i-- instead of i++ puts my text in the textArea.
But it doesn't want to repeat the word with the number I provide in the second textField.
This image shows the program. When I type a number in the Repetition textField, I want the text to repeat times the number I provided. So if I type 4, it should repeat 4 times.
Sorry for my english.

What kind of 'for' loop is this?

My friend gave me this Arduino code:
int button;
void setup(){
pinMode(12, INPUT);
}
void loop(){
for(button; button == HIGH; button == digitalRead(12)) { //This line
//Do something here
}
}
The line commented with "this line" is unclear to me.
I've always seen a for loop like:
for (init; condition; increment)
Also used in different ways, like:
for(int i=0; i<n; i++){}
for(;;){}
And so on, but I've never seen something like the code I got from my friend.
It does compile on the Arduino IDE, so what is the meaning of this specific for loop?
In other words, what kind of loop is it, and how does it work?
This loop:
for(button; button == HIGH; button == digitalRead(12))
is equivalent to:
button; // does nothing - should probably be `button = HIGH;` ?
while (button == HIGH) // break out of loop when button != HIGH
{
//do something here
button == digitalRead(12); // comparison - should probably be assignment ?
}
Note: I suspect the whole loop is buggy and should probably read:
for (button = HIGH; button == HIGH; button = digitalRead(12))
// do something here
Firstly, let's interpret this literally. Converts to while loop as:
button; // does nothing
while(button == HIGH) { // clear
// do stuff
button == digitalRead(12); // same as digitalRead(12);
}
This code really should be setting off a lot of IDE or compiler warnings. Anyway my answer is correct, that's what it literally converts to. Note that button == digitalRead(12) is valid but does nothing with the result of the comparison.
Most likely the code is buggy. One hypothesis is the == should be =.

Cannot read and write to the same file in an exported Processing app

I am using an external .txt file to save the incrementing name index for whenever someone "takes a picture" in my app (i.e. image_1.jpg, image_2.jpg, etc...). I am trying to save the data externally so that a user does not overwrite their pictures each time they run the program. However, because of the way that Processing packages its contents for export I cannot both read and write to the same file. It reads the appropriate file located in the apps package contents, however, when it tries to write to that file, it creates a new folder in the same directory as the app itself and writes to a new file with the same name instead.
Essentially, it reads the proper file but refuses to write to it, instead making a copy and writing to that one. The app runs fine but every time you open it and take pictures you overwrite the images you already had.
I have tried naming the "write to" location the explicitly same link as where the exported app stores the data folder inside the package contents (Contents/Resources/Java/data/assets) but this creates a copy of this directory in the same file as the app.
I have also tried excluding the file I am trying to read/write from my data folder when I export the app by changing the read code to ../storage/pictureNumber.txt and then putting this file next to app itself. When I do this the app doesn't launch at all because it is looking in its own data folder for storage and refuses to go outside of itself with ../ . Has anyone had luck both reading from and writing to the same file in an exported processing .app?
Here is the code for the class that is handling the loading and saving of the file:
class Camera {
PImage cameraImage;
int cameraPadding = 10;
int cameraWidth = 60;
int opacity = 0;
int flashDecrementer = 50; //higher number means quicker flash
int pictureName;
Camera() {
String[] pictureIndex = loadStrings("assets/pictureNumber.txt");
pictureName = int(pictureIndex[0]);
cameraImage = loadImage("assets/camera.jpg");
String _pictureName = "" + char(pictureName);
println(pictureName);
}
void display(float mx, float my) {
image(cameraImage, cameraPadding, cameraPadding,
cameraWidth, cameraWidth-cameraWidth/5);
}
boolean isOver(float mx, float my) {
if (mx >= cameraPadding &&
mx <= cameraPadding+cameraWidth &&
my >= cameraPadding &&
my <= cameraPadding+cameraWidth-cameraWidth/5) {
return true;
}
else {
return false;
}
}
void captureImage() {
save("pictures/"+lines.picturePrefix+"_"+pictureName+".jpg");
pictureName++;
String _null = "";
// String _tempPictureName = _null.valueOf(pictureName);
String[] _pictureName = {_null.valueOf(pictureName)};
saveStrings("assets/pictureNumber.txt", _pictureName);
println(_pictureName);
}
void flash() {
fill(255, opacity);
rect(0,0,width,height);
opacity -= flashDecrementer;
if(opacity <= 0) opacity = 0;
}
}
After a lot of searching I found that you have to use savePath() in order to read from a directory outside of the project.jar. The camera class constructor now looks like this:
path = savePath("storage");
println(path);
String[] pictureIndex = loadStrings(path+"/pictureNumber.txt");
pictureName = int(pictureIndex[0]);
cameraImage = loadImage("assets/camera.jpg");
String _pictureName = ""+char(pictureName);
and everything works!

Resources