weird fileInput.eof( ) behaviour and workaround !?

39 views
Skip to first unread message

Kaj88

unread,
Aug 19, 2015, 5:10:03 AM8/19/15
to Haxe
I am currently learning Haxe and played around with reading text files.

When using the fileInput.eof() function, I recognized some weird behaviour. I used readLine() and readBytes() to read from the file.

# readLine()
When using eof() in combination with readLine() it will do it's job only if the last line does contain at least one character (or: the last character in the file cannot be a new line character). If the last character in the file is a new line character, EOF will be thrown.

# readBytes()
eof() in combination with readBytes() does not work at all. It always throws a EOF. For the workaround I use
sys.FileSystem.stat(path).size to figure out the total file size in bytes. Then I use fileInput.tell() to get the number of bytes read from the file. When the number of bytes read equal the total file size in bytes, all contents have been read.
Do you think this is a good and valid workaround?

All functions and behaviour has been tested using "haxe -main ReadFile --interp" on MacOSX. Other targets and platforms are not tested yet.

the code:
// ReadFile.hx

import haxe.io.Bytes;
import sys.io.File;

class ReadFile {
   
static public function main() {
       
var path = "./datei.txt";

       
/* throws EOF only if the last line does not contain
        at least one character */

       
// readWith_readLine(path);

       
// always throws EOF
       
// readWith_readBytes(path);
       
       
// works with "haxe -main ReadFile --interp"
        readWith_readBytes_workaround
(path);
   
}

   
static function readWith_readLine(path) {
       
var fileInput = File.read(path, true);

       
var stringBuffer = new StringBuf();
       
while (!fileInput.eof()) {
            stringBuffer
.add(fileInput.readLine());
       
}
        fileInput
.close();

       
var string = stringBuffer.toString();
       
Sys.print(string);
   
}

   
static function readWith_readBytes(path) {
       
var bufferSize = 64;
       
var buffer = Bytes.alloc(bufferSize);
       
var fileInput = File.read(path, true);

       
var available;
       
var stringBuffer = new StringBuf();
       
while (!fileInput.eof()) {
            available
= fileInput.readBytes(buffer, 0, bufferSize);
           
var readString = buffer.getString(0, available);
            stringBuffer
.add(readString);
       
}
        fileInput
.close();

       
var string = stringBuffer.toString();
       
Sys.print(string);
   
}

   
static function readWith_readBytes_workaround(path) {
       
var bufferSize = 64;
       
var buffer = Bytes.alloc(bufferSize);
       
var fileInput = File.read(path, true);
       
var totalNumberOfBytes = sys.FileSystem.stat(path).size;

       
var available;
       
var stringBuffer = new StringBuf();
       
while (totalNumberOfBytes != fileInput.tell()) {
            available
= fileInput.readBytes(buffer, 0, bufferSize);
           
var readString = buffer.getString(0, available);
            stringBuffer
.add(readString);
       
}
        fileInput
.close();

       
var string = stringBuffer.toString();
       
Sys.print(string);
   
}
}




Reply all
Reply to author
Forward
0 new messages