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.
Page Index Toggle Pages: 1
file I/O (Read 1009 times)
file I/O
Jul 7th, 2009, 10:55am
 
Hi,
  I want to learn how to open a file - write to it - close it - and perhaps open it later and add something to it. Like something you would like to do when logging data from something you read off an Arduino board. Can anyone show me a simple example of this?

I am a beginner, oh verily....
Re: file I/O
Reply #1 - Jul 7th, 2009, 12:02pm
 
Sounds like you need loadStrings() and saveStrings()...
Re: file I/O
Reply #2 - Jul 11th, 2009, 4:00am
 
ahhh - I see; to achieve the 'append' feature I am looking for, first read in what is in the file already, then add to this, then write out?

I tried this, but I am obviously missing on several cylinders - here is code that I thought would work. The error message I get is "Syntax error, maybe a missing right parenthesis?" and the line

fileAppend(String filename,String words);

is highlighted. I don't think that is the problem - maybe I am miles out and the error message is just one of several possibilities. I guess I need more help, or a pointer to a place where this stuff is covered.

Cheers, Peter.

// Test of adding text to a file if it exists

void setup() {
//
}

void draw() {
//

string filename= "nouns.txt";
string words = "apple bear cat dog";
fileAppend(String filename,String words);
}

void fileAppend() {
// first try to read from file
//
String filestuff = loadStrings(filename);
// then, depending on what was found, do either ...
if (filestuff == null) {
// file is empty, just write words out, one per line
String[] list = split(words, ' ');
saveStrings(filename, list);
}
// or ...
else {
// file has stuff in it, append words to filestuff, and write all of it out

// ... more code later
println("Pretend writing ...");
 
}
}
Re: file I/O
Reply #3 - Jul 11th, 2009, 7:23am
 
Code:

/*
draw() gets called automatically several times a second. So depending on what you want to do, you should put your call to fileAppend() in a more relaxed place, like setup (but then, that depends on what you want to do)
*/
void setup() {

/*
When you declare a variable to be of type String, the word String should be capitalized. String is a class in Java / Processing, class names are always capitalized.
*/
 String filename= "nouns.txt";
 String words = "apple bear cat dog";

/*
This is a method call. Just put the names of the variables in here, without their types.
*/
 fileAppend(filename, words);
}

/*
This is the method definition, put the types of the parameters here
*/
void fileAppend(String filename,String words) {

 /* loadstrings returns an array of Strings */
 String[] filestuff = loadStrings(filename);

 // then, depending on what was found, do either ...
 ....
}
Re: file I/O
Reply #4 - Jul 11th, 2009, 7:34am
 
Thanks! I am new to Processing, and it seems a long way to go to write to a file Wink I must be trying to do something that is out there as example code already?

Thanks for the help - I will post the code once it is finished and works.

Cheers,  Peter
Re: file I/O
Reply #5 - Jul 11th, 2009, 10:34am
 
All right, here is my first attempt at setting up a Processing sketch that will allow the addition of words at the end of a preexisting file:

// Test of adding text to an existing file

void setup() {
//

String filename= "nouns.txt"; // Name of the file - which must exist in sketch folder
String words = "straw tree bush"; // List of words to add to the file
fileAppend( filename, words); // The call
}

void fileAppend(String filename,String words) {
 
 /*
 
 Processing routine to split 'words' into a list, and add
 them on seperate lines at the end of the file 'filename'.
 
 */
 
// Split input words into a list
String[] list = split(words, ' ');
// read from (necessarily existing) file
String[] filestuff = loadStrings(filename);
//
String[] newlist = new String[list.length+filestuff.length];
// add old stuff to new list
for(int i=0; i < filestuff.length; i++) {
 newlist[i] = filestuff[i];
}
// Add new stuff to new list
for(int i=0; i < list.length; i++) {
 newlist[i+filestuff.length] = list[i];
}
// then write complete array to file
saveStrings(filename, newlist);
}


I still need to work out how to do the above but with numbers (integers and floats) and what to do when the file does not already exist. Thanks for the help I received!

Peter
Re: file I/O
Reply #6 - Jul 11th, 2009, 11:15am
 
it compiles! let's ship it!
no seriously pretty cool for a first attempt.
Re: file I/O
Reply #7 - Jul 11th, 2009, 1:20pm
 
Thanks Wink

At one point I had : String newlist[] =
instead of: String[] newlist =
and it worked fine - is that a bug or a feature of Processing?

Re: file I/O
Reply #8 - Jul 11th, 2009, 3:13pm
 
both notations mean exactly the same thing. It probably has some deep historical reasons or maybe somebody made a typo. Anyway the general declaration of an array goes:

Type[] name = new Type[number];
or
Type name[] = new Type[number];
Page Index Toggle Pages: 1