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 › Problem with loadStrings[]
Page Index Toggle Pages: 1
Problem with loadStrings[] (Read 803 times)
Problem with loadStrings[]
Oct 8th, 2009, 3:01pm
 
Hello,

I am trying to do a simple loadStrings from a .txt file I have placed in the data file. My code is as follows:

Code:
void setup() {
 size(400, 400);
 smooth();
 strokeWeight(20.0);
 stroke(0, 100);
}

void draw() {
 background(226);
}

String affirmation[] = loadStrings("affirm.txt");
for (int i=0; i < affirmation.length; i++) {
 println(affirmation[i]);
}


The program draws the window and background, but does not println the strings. Instead, I get an error that says the URL or file is inaccessible or unreadable.

I have confirmed that the file is named correctly ("affirm.txt") and has been placed in the data file.

Any suggestions? Thanks.
Re: Problem with loadStrings[]
Reply #1 - Oct 8th, 2009, 3:46pm
 
Silly question, but just to confirm:
your folder heirarchy is as follows:

folder [Sketchname]:
  [Sketchname].pde
  folder "data":
      affirm.txt

        ?
Re: Problem with loadStrings[]
Reply #2 - Oct 8th, 2009, 6:30pm
 
the problem is another one.

look where you put it.
loadstring has to be in the setup. declare the variable outside to make it global...

and put the for loop in the draw, right now its somwhere it doesnt belong.
like this it is working:

String affirmation[] ;

void setup() {
 size(400, 400);
 smooth();
 strokeWeight(20.0);
 stroke(0, 100);
 affirmation = loadStrings("affirm.txt");
}

void draw() {
 background(226);
 for (int i=0; i < affirmation.length; i++) {
 println(affirmation[i]);
}

}
Re: Problem with loadStrings[]
Reply #3 - Oct 8th, 2009, 7:53pm
 
Oh yes.  Alternately, get rid of setup() and draw() -- you can have code outside of functions as long as you don't have any functions.
so, running just the following will also work:
Quote:
String affirmation[] = loadStrings("affirm.txt");
for (int i=0; i < affirmation.length; i++) {
 println(affirmation[i]);
}
Page Index Toggle Pages: 1