SaveString() overwrite text in .txt file

Hi everyone.

I receive single words form a microphone input that I will like to save in a text file.
I manage to save one word in the text file, but when i say a new word the old one get overwritten.

How can I add the new word after the one(s) already been saved?

thanks for help

String wordFile = "MyText.txt";

void webSocketServerEvent(String msg) {
  println(msg);
  String[] wordFile = split(msg, ' ');
  saveStrings("data/MyText.txt", wordFile);
  loadStrings("data/MyText.txt");
}
1 Like

I wonder if adding ### to the file name will help
saveStrings(“data/H-Farm######.txt”, wordFile);

1 Like

@PascalAudet I guess I’m not getting you. Can you please be more specific?

I dont think it works like saveFrame() does:

With saveFrame() you can specify the name of the sequence with the filename parameter, including hash marks (####), which will be replaced by the current frameCount value. (The number of hash marks is used to determine how many digits to include in the file names.)

But you can do this if you do not what to overwrite your file

int name=0;  
void draw()
{
String words = "apple bear cat dog";
String[] list = split(words, ' ');

// Writes the strings to a file, each on a separate line
saveStrings("nouns-"+str(name++)+".txt", list);

}

Thank you for explaining that, I tried but what he did is creating another file named H-Farm######.txt in the data folder of the sketch and the behavior is the same, it jusr write one word and keep overwriting it.

I try now with the suggestion you give me! Thank you

I’m sorry, but is not clear to me in the exemple you give me where exactly do I declare in witch text file the code should save the word(s).

In the way I show below it dose not work.

int name=0;  

void webSocketServerEvent(String msg) {
  String[] wordFile = split(msg, ' ');
  saveStrings("nouns-"+"MyText"+".txt", wordFile); // Writes the strings to a file, each on a separate line

Like this:

saveStrings("nouns-"+str(name++)+".txt", wordFile); 

this will give you a list of file like:
nouns-0.txt
nouns-1.txt
nouns-2.txt
nouns-3.txt
nouns-4.txt
nouns-5.txt
nouns-6.txt
nouns-7.txt
nouns-8.txt
nouns-9.txt
nouns-10.txt

Ah wait, maybe i wrote something that was misleading.
I don’t want to create multiple text files. What I would like to get is only one text file with a list of the words that has been recorder from the microphone.
sorry about that

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