• Writing to a text file

    From Lab Rat@TOXIC to All on Thursday, February 06, 2014 23:55:22
    Hi all,

    Unfortunately, Javascript isn't my first (programming) language and I'm struggling to get to grips with creating a simple plain text file from a script. I've spent hours staring at the JS Object Model Reference (http://synchro.net/docs/jsobjs.html) and I just cannot work it out.

    This is what I've got so far:

    var f = new File('test.txt');
    file.open('w');
    file.writeln('Line 1');
    file.writeln('Line 2');
    file.close();

    I've tried the file.open(file_exists(file.name) ? 'r+':'w+'); line mentioned
    in the documentation and I just cannot get this to work.

    Any code suggestions would be welcome!

    Thanks,

    Lab Rat.


    ---
    ■ Synchronet ■ Toxic Laboratory BBS - Birmingham, UK - toxicbbs.com
  • From Digital Man to Lab Rat on Thursday, February 06, 2014 17:46:13
    Re: Writing to a text file
    By: Lab Rat to All on Thu Feb 06 2014 11:55 pm

    Hi all,

    Unfortunately, Javascript isn't my first (programming) language and I'm struggling to get to grips with creating a simple plain text file from a script. I've spent hours staring at the JS Object Model Reference (http://synchro.net/docs/jsobjs.html) and I just cannot work it out.

    This is what I've got so far:

    var f = new File('test.txt');
    file.open('w');
    file.writeln('Line 1');
    file.writeln('Line 2');
    file.close();

    "file" is not defined in that example. I think you meant to write f.open(), f.writeln(), and f.close().

    I've tried the file.open(file_exists(file.name) ? 'r+':'w+'); line
    mentioned in the documentation and I just cannot get this to work.

    Replacing "file" with "f" in your example seems to work fine for me (creates a file "test.txt" with the 2 lines you have specified).

    Any code suggestions would be welcome!

    Your code looks fine (except for the file/f confusion I mentioned). It might be
    confusing to you that the file being created/written will be located in your Synchronet "ctrl" directory since you did not specify a full pathname for the file.

    There are many examples in the exec/*.js files which open/modify files. You could look at those for some tips or inspiration as well.

    digital man

    Synchronet "Real Fact" #3:
    Synchronet version 3 is written mostly in C, with some C++, x86 ASM, and Pascal.
    Norco, CA WX: 50.6°F, 92.0% humidity, 2 mph W wind, 0.12 inches rain/24hrs
  • From MCMLXXIX@MDJ to Lab Rat on Thursday, February 06, 2014 21:09:09
    Re: Writing to a text file
    By: Lab Rat to All on Thu Feb 06 2014 23:55:22

    var f = new File('test.txt');
    file.open('w');
    file.writeln('Line 1');
    file.writeln('Line 2');
    file.close();

    this actually looks fine to me... you can try checking whether or not the file.open() was successful ala var didItOpen = file.open('w');

    ---
    ■ Synchronet ■ The BRoKEN BuBBLE (bbs.thebrokenbubble.com)
  • From Lab Rat@TOXIC to Digital Man on Friday, February 07, 2014 11:02:41
    Re: Writing to a text file
    By: Digital Man to Lab Rat on Thu Feb 06 2014 17:46:13

    "file" is not defined in that example. I think you meant to write f.open(), f.writeln(), and f.close().

    Ah, that makes a lot of sense! See, Java isn't my best programming language!

    There are many examples in the exec/*.js files which open/modify files. You could look at those for some tips or inspiration as well.

    I tried looking through a good few but being all wrapped up in functions with arrays, I just got even more confused - but makes sense now.

    Thanks DM, all working fine now. Most appreciated.

    Lab Rat.


    ---
    ■ Synchronet ■ Toxic Laboratory BBS - Birmingham, UK - toxicbbs.com
  • From Lab Rat@TOXIC to MCMLXXIX on Friday, February 07, 2014 11:05:58
    Re: Writing to a text file
    By: MCMLXXIX to Lab Rat on Thu Feb 06 2014 21:09:09

    this actually looks fine to me... you can try checking whether or not the file.open() was successful ala var didItOpen = file.open('w');

    As DM said, I needed to use the variable that I had declared as a file, not just the word 'file'. All working now though, thanks for the reply!

    Lab Rat.


    ---
    ■ Synchronet ■ Toxic Laboratory BBS - Birmingham, UK - toxicbbs.com
  • From MCMLXXIX@MDJ to Lab Rat on Friday, February 07, 2014 11:29:48
    Re: Writing to a text file
    By: Lab Rat to MCMLXXIX on Fri Feb 07 2014 11:05:58

    As DM said, I needed to use the variable that I had declared as a file, not just the word 'file'. All working now though, thanks for the reply!

    well, it's good that he was paying attention.. clearly i wasn't :D

    ---
    ■ Synchronet ■ The BRoKEN BuBBLE (bbs.thebrokenbubble.com)
  • From Nightfox@DIGDIST to Lab Rat on Friday, February 07, 2014 11:18:30
    "file" is not defined in that example. I think you meant to write f.open(), f.writeln(), and f.close().

    Ah, that makes a lot of sense! See, Java isn't my best programming
    language!

    That code is JavaScript, not Java.

    Nightfox

    ---
    ■ Synchronet ■ Digital Distortion BBS - digitaldistortionbbs.com
  • From Deuce@SYNCNIX to Lab Rat on Friday, February 07, 2014 11:39:30
    Re: Writing to a text file
    By: Lab Rat to All on Thu Feb 06 2014 11:55 pm

    var f = new File('test.txt');
    file.open('w');
    file.writeln('Line 1');
    file.writeln('Line 2');
    file.close();

    I've tried the file.open(file_exists(file.name) ? 'r+':'w+'); line
    mentioned in the documentation and I just cannot get this to work.

    Since you're not specifying a path, I think it goes in the ctrl directory.

    ---
    http://DuckDuckGo.com/ a better search engine that respects your privacy.
    ■ Synchronet ■ My Brand-New BBS (All the cool SysOps run STOCK!)
  • From Lab Rat@TOXIC to Deuce on Saturday, February 08, 2014 13:13:00
    Re: Writing to a text file
    By: Deuce to Lab Rat on Fri Feb 07 2014 11:39:30

    Since you're not specifying a path, I think it goes in the ctrl directory.

    I was looking all over for it (including ctrl directory) and it wasn't working. All working fine now with the f... (or whatever I declare) as opposed to file...

    Lab Rat


    ---
    ■ Synchronet ■ Toxic Laboratory BBS - Birmingham, UK - toxicbbs.com