We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › reading string value back from a file problem
Page Index Toggle Pages: 1
reading string value back from a file problem (Read 183 times)
reading string value back from a file problem
Mar 2nd, 2009, 9:52pm
 
Hi all,
I'm trying to figure out the best way to save/load all the variables states in my program to a file, and then load them back in, like a 'preset'. I've been using savestrings() which works fine for numbers using str() and int() to convert back and forth, however when I save/load a string back into a string there is a problem.

Here's a test program to demonstrate.

void setup() {
 //  writer();
 reader();
}
void draw() {
}

void writer() {
 String words = "apple bear cat dog";
 String[] list = split(words, ' ');

 // now write the strings to a file, each on a separate line
 saveStrings("nouns.txt", list);

}


void reader() {
 String lines[] = loadStrings("nouns.txt");
 println("there are " + lines.length + " lines");
 for (int i=0; i < lines.length; i++) {
   println(lines[i]);
   if (lines[i]=="cat") {
     println("i hate them");
   }
 }
}


so when I read the file back in, I can't get processing to recognise "cat" as the string it was written in, i.e. "i hate them" doesn't print for some reason!

Re: reading string value back from a file problem
Reply #1 - Mar 2nd, 2009, 11:16pm
 
Aaah, I want 1€ each time I see this problem! Wink

Look at == (equality) reference, they have a word about using it with strings...
Re: reading string value back from a file problem
Reply #2 - Mar 2nd, 2009, 11:25pm
 
Great!
I knew it would be something simple and stupid, but usually these are the hardest to figure out sometimes.

p.s. i dont hate cats.
Page Index Toggle Pages: 1