Getting the size of a file and rewinding a file?

Posted on
  • Not sure if this is the right place to ask, but I am recently involved in a pet project (will certainly post more on that when it's ready :)) that would benefit from getting the file size of a particular file as well as "rewinding" a file pipe to the very beginning. For the latter function currently what I am doing right now is to close the file handle and reopening at the same path so to mimic this behavior, but for now I really have no idea how to get the size of a particular file.

    Would there be a future API update for these? Or is it that it is in fact possible at this stage that I happened to miss? Thanks!

  • Hi,

    Right now you'll have to find out the file size by just reading bytes until you reach the end of the file I'm afraid. I've put an issue on GitHub for it though, and will try and add something.

    To try and stay like node.js, I'll probably just use something like require('fs').statSync("myfile.txt")

    For rewinding, you'll have to reopen the file (as you're doing). I'm not sure re-opening it is that much slower than a seek, but I'll look at adding one.

  • Pardon my late reply. It sounds like a good news! Would definitely be looking forward to both of these two being implemented, if possible.

    If we can make it both possible, then probably we'd benefit from getting the current reading position as well (similar in behaviour to ftell for instance), rather than storing an additional variable to cache the seeking position. What do you think?

  • At the moment it's still a battle to keep the size of Espruino's firmware down, so I'm afraid I'll leave ftell for now - I've basically got to save space for functionality that expands what Espruino can do, rather than just stuff that's 'nice to have'.

    At some point I'll switch to size optimisation, which will hurt performance a bit but will give an extra 50kB or so to cram everything into.

  • Indeed. BTW, just saw that the fs.stat and File.seek are now on the repo. Perhaps should try it out later tonight if I get the chance to.

    Thanks anyway!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Getting the size of a file and rewinding a file?

Posted by Avatar for Kelvin @Kelvin

Actions