SaveString() overwrite text in .txt file

Something like:
Funny
Blue
Dance
… and so on…

what happen now with the code is that i save: “Funny” and then when I save “Blue” it overwrite the word funny instead of having something like:
Funny
Blue

OK in this case try this:
https://processing.org/reference/createWriter_.html

sorry, i think that the point here might be
-a- first need to read the already existing strings from the file
-b- then need to extend that array
-c- then need to add the new word from the new split array one by one
-d- and write all to the same file again.
? is that the function what you need?

Hi kll, yes you get it right to the point.
I’m not able to do it by my own as I’m a beginner. Could you please provide me some documentation or
example on how i could build such a function?
Thank you

like?

  String filename ="data/MyText.txt";
  String[] lines;

   lines = loadStrings(filename);

  String[] newlines = split(answer, ' ');
  for ( String l : newlines ) {
    expand(lines); 
    lines = append(lines, l);  // add one line for each new word
  }
  saveStrings(filename, lines);

sorry raw copy out of my test

if the file not exists it is more tricky

2 Likes

yes that definitely looks promising and more suitable for my needs. Thanks a lot.
I will integrate it in my code and test it. I will let you know the result.

Thanks again for the help!

sorry i not know that one, is that able to open existing file for write / append?

one more question where do I declare the variable “lines”?
now I get the error " the variable lines does not exist"

sorry, i edit, check again

1 Like

but i just read what you actually try to do, the word-cloud thing?
so you should first loop through the “existing” list and check if the word is there already?
and only if NOT add it?

https://processing.org/reference/String_equals_.html

Hey, first of all thank you al lotfor your help, now it works perfect. :slight_smile:

Yes i’m try to do the word-cloud.
Actually the text at the beginning is empty (no word) then people can speak to the microphone and the word they say will be displayed. The more the same word has been say then bigger it will appear.

I’m editing a code i’ve found online and i’m struggling to understand how it works. Originally it upload a text, he count the words and display the cloud.

I need that he start from 0 words and then start adding them and resize them live, while running

-a- well you can start from a empty file,
-b- still new words should be checked for have no double
( i tell you that arrays are not funny, like deleting a double later is tricky )
-c- but reading a not existing file gives error ( so you could create one )

actually that is a big problem, easy we can exchange code here at the forum
but as soon files ( and subdir ) are involved the sharing is not so easy
( i can give you a zip, but would you want download a zip from a unknown source? )

No it does not append

1 Like

I think this code is a good challenge for me to learn, I will follow your indication like starting from an empty file.
I think is ok to have double so that the code can know witch one is the most “used” word otherwise they will be all the same size, no?

Yes reading a empty file give error but i was thinking to solve that problem later…

Yes I would download the zip from you. But I don’t want you to do the code for me I would like to learn how to develop it by my own.

actually i think there might come much more functionality to this,
so i doubt that on long terms you will be happy with that text file.
a database? how is your SQL?
processing works great with sqlite3, and same easy about file handling
like that text file you use now.
( can not compare with the install and use of MySQL 8 )

Well the code I’m trying to develop is for an installation. I’ll use only once for one evening. I’m not an expert and so I trust what you say but creating a DB it seams to me not necessary for the use I will do of it. No?

Moreover I use SQL only once…

Not sure if this fits the case, but maybe there’s no need to call expand nor append. Maybe concat is enough? concat returns a new array that’s the combination of two arrays.

  String filename ="data/MyText.txt";
  String[] oldlines = loadStrings(filename); // load old words into a String array

  String answer = "bicycle flower"; // simulated answer
  String[] newlines = split(answer, ' '); // split answer into words (a String array)
  
  saveStrings(filename, concat(oldlines, newlines)); // save both arrays to the file

But if you know that the answer only contains one word, then there’s no reason to split it by spaces, and it’s even shorter:

  String filename ="data/MyText.txt";
  String[] lines = loadStrings(filename);

  String answer = "bicycle"; 
  
  saveStrings(filename, append(lines, answer)); // save the old array + the new word

Which just adds one word to the file.

Now you should be able to create a function that adds a word to a file. It would look like this:

void addWordToFile(String word, String filename) {
  // I leave this as an exercise :)
}

You could call it like this:

addWordToFile("orangutan", "data/MyText.txt");

:slight_smile:

1 Like

Hey Hamoid
thanks for the elegant hint! It work out! :slight_smile:

this is the code

String wordFile = "data/H-Farm.txt";
String[] msg;

void webSocketServerEvent(String msg) {  //listen the input from microphone and add it in the string call msg
  println(msg); // print the line with voice input
  addWordToFile(msg, "data/H-Farm.txt"); // call addWordToFile
}

void addWordToFile(String msg, String wordFile) { // create two strings: msg and wordFile
    String[] oldlines = loadStrings(wordFile); // create a string (empty) call old.. and i fill it with what there's already in the text file
    String answer = msg ; // not clear what this line does.. 
    String[] newlines = split(msg, ' '); // create string and fill it with string "msg" splitted every empty space
    saveStrings(wordFile, concat(oldlines, newlines)); //save all 
}

I don’t understand why I am creating so many strings (msg, wordFile, oldlines, answer, newlines) those are all string right?
I comment each line with what i guess the code does, would you mind please to check my comment and see if they are right?
thank you a lot
as well for the exercise :wink:

Hi :slight_smile: Good that it works :slight_smile:

You have indeed a few extra variables.

  • You could delete the first two lines, because you are not using them anywhere. They are global variables, and they get overwritten by local variables with the same names.
  • After void addWordToFile I would not write “create two strings”. They are actually arguments / parameters for the addWordToFile function. The values of those two variables are received from outside, from wherever you are calling the function.
  • You can also delete String answer = msg; since you are not using answer anywhere.

@GoToLoop in the guidelines it recommends not to post links or code without explanations :slight_smile:

2 Likes