(PB) FileToStringEx – FileToString – Helper functions

Posted by on December 22, 2014

Intro:

Here’s a small but useful io function, The basic routine is also included for less demanding uses.

This function will read a text file into a string, the Ex (extended) functionality allows for defining size constraints to the return stringĀ and positioning of the file pointer for location/seeking purposes.

On big text files and in cases where you want to limit memory usage, this is a very useful routine. Otherwise the basic function works just fine.

The extended routine can also be used with a callback to set a progress bar, based on data size you can also calculate the remaining time, however that’s all beyond the scope of this post.

Code:

Use example:

Keep in mind that with the basic routine the whole file will be read into memory, so you have to beware of this detail.

If you never expect file sizes to exceed a certain range, this won’t be an issue. Otherwise use the Ex function to prevent memory and other performance related issues.

If the file is not found the return string is empty, this is the expected behavior, no error codes are used but you can easily implement this.

Have fun!