Folks,
Trying to write a simple juce application to change a substring within a text file. It looks like the word gets replaced, but when I call File::replaceWithText(), there are extra bytes inserted into the text file, and the file can’t be parsed properly. Perhaps I am just not understanding the correct usage of File::replaceWithText.
File configText;
configText = T("config.txt");
if(configText.existsAsFile())
{
// load current config.txt into memory as a String
String currentConfig = configText.loadFileAsString();
// print the current string length before doing anything
int len = currentConfig.length();
printf("currentConfig len: %d\n",len);
// change substring within string
String newConfig = currentConfig.replace(T("text1"),T("text2"),0);
// print new string length
len = newConfig.length();
printf("newConfig len: %d\n",len);
// write new config.txt to a new file
configText.replaceWithText(newConfig);
// now print the new file length - somehow the file size increases
int filelen = configText.getSize();
printf("filelen: %d\n",filelen);
}
This is a simple 18 line file. After running the application, the file still contains 18 lines and looks identical to the human eye, but the size increases from 463 bytes to 481, as shown below. It appears that the file size is increasing after the File::replaceWithText() but the String::replace() is not increasing the size of the file:
src# ./testapp
currentConfig len: 463
newConfig len: 463
filelen: 481
src#
