Haxe Forum > File writing with haxe

  • Hello world!

    I was wondering how I could get haxe to write something in a file, I plan on targeting cpp, so I checked the libraries and found "cpp.io.File", but it's not very descriptive. Basically, I want to be able to get my program to write a file on the hard drive. Anyone have any suggestions?

    Thanks!

  • Some backend aspects like this work the same in many backend targets, php, neke, c++ and eventually Java and csharp, so the tutorials for neko often cover the details, for example:
    http://haxe.org/doc/neko/fileio
    Haxe 3 is moving towards standardizing this into a new cross platform package sys so for instance look at:
    http://haxe.org/api/sys/io/file
    Let us know how you get on but I found that PHP and Neko file writing was the same just a remap so I expect c++ will also be the same, although to future proof your work try to use the sys package.

  • Main.hx

    class Main
    {
        public static function main()
        {
            sys.io.File.saveContent("./test.txt", "hello world");
        }
    }

    build.hxml
    -main
    -cpp bin

    Executing Main(.exe) in the bin folder will save the text "hello world" to file test.txt inside the same folder where Main.exe is located/executed.

    Jan

  • OK, so that tells me how to create a file with something in it, what if I'm adding to what's already in the file?

  • Please re-read my advice and then read the neko tutorials.
    http://haxe.org/doc/neko/

  • Main.hx

    class Main
    {
        public static function main()
        {
            var f = sys.io.File.append("./test.txt");
            f.writeString("additional text");
            f.close();
        }
    }

    build.hxml
    -main Main
    -cpp bin

  • This link maybe useful http://haxe.org/doc/sys/ it was written while 'sys' package was being added to haxe, so details may have changed.

  • Oh, so that's what FileIO meant. OK, I think I have this now. Thanks!

< Prev | Page 1 / 1 | Next >