jslib and appending files

Talk about add-ons and extension development.
Post Reply
dangbat
Posts: 14
Joined: December 18th, 2002, 1:00 am

jslib and appending files

Post by dangbat »

Here is the code I'm trying out (the file test.txt already exists):

function testAppendO() {
var f=new File('c:\\test.txt');
f.open('a');
f.write('This is a new line #1\n');
f.write('this is a new line #2\n');
f.close();
f.open('a');
f.write('This is a new line #3\n');
f.write('this is a new line #4\n');
f.close();
}

I just put this so that it happens when the extension is loaded (so, once the browser starts). The contents of test.txt will end up being

This is a new line #3
this is a new line #4

Am I doing something seriously wrong here? If I understand the concept of appending a file, the contents should remain unchanged, correct? It seems that this is reacting as if I used f.open('w'); instead. One workaround that I have found is to f.open('r'), then contents=f.read(), then open it up again, and write(contents). However, that doesn't always work (sometimes it thinks the contents is null, even though if I do an alert, they show up as the file contents). This workaround does not work for onload events, for instance, just for the initial browser load.

I am not stuck on using jslib, but I really need to be able to append files. Honestly, I have seen very little documentation on this, but if there is a tutorial (other than the ONE XUL tutorial) for creating/writing files with javaScript, it would be great to know about it. Just understanding this particular bug/issue would be enough, though.

-D

System: windows 2000, Phoenix 0.5, jslib build from 1/15/03

PS: If this is something that I should be asking in the mozilla forums as well, please let me know.
User avatar
alanjstr
Moderator
Posts: 9100
Joined: November 5th, 2002, 4:43 pm
Location: Anywhere but here
Contact:

Post by alanjstr »

Well, I don't see anything in there that specifies the open mode (read only, read write, append, etc). It appears that it is just doing a write mode, overwriting anything already there.
Former UMO Admin, Former MozillaZine General Mod
I am rarely on mozillaZine, so please do not send me a private message.
My Old Firefox config files
dangbat
Posts: 14
Joined: December 18th, 2002, 1:00 am

Post by dangbat »

f.open('a');

According to the documentation and the jslib website, that's how you open it in APPEND mode, f.open('w') would be Writing, f.open('r') would be reading. Am I misreading the example (http://jslib.mozdev.org/libraries/io/file.js.html)

Is there something ELSE that I have to do before I get this working?

Thanks,
-D
User avatar
alanjstr
Moderator
Posts: 9100
Joined: November 5th, 2002, 4:43 pm
Location: Anywhere but here
Contact:

Post by alanjstr »

Where is jslib? Is that a part of Mozilla?
Former UMO Admin, Former MozillaZine General Mod
I am rarely on mozillaZine, so please do not send me a private message.
My Old Firefox config files
cdn
Posts: 999
Joined: November 4th, 2002, 5:47 pm
Location: UK
Contact:

Post by cdn »

alanjstr wrote:Where is jslib? Is that a part of Mozilla?


might be now, I thought I saw it get checked out once

otherwise : http://jslib.mozdev.org/
dangbat
Posts: 14
Joined: December 18th, 2002, 1:00 am

Post by dangbat »

Well, as I mentioned in my original posting, I am not stuck in using jslib, but would prefer it, considering how much easier it would make my life.

So, I guess my question now is threefold.
1. Anyone know what the bug is?
2. Should I be asking this in the Mozilla forums?
3. How would I open a file for appending and write to it without using jslib (ie, the 'regular' way, whatever that is)?

Thanks for the responses so far,

-D
User avatar
alanjstr
Moderator
Posts: 9100
Joined: November 5th, 2002, 4:43 pm
Location: Anywhere but here
Contact:

Post by alanjstr »

Sounds like a Mozilla problem to me.
Former UMO Admin, Former MozillaZine General Mod
I am rarely on mozillaZine, so please do not send me a private message.
My Old Firefox config files
Post Reply