Reading a file.

Started by KAKAN, Dec 08, 2015, 08:54 AM

Previous topic - Next topic


Hello, I created a file( info.txt ) which containsThis is a text file!!I want to read and print this file from the server
Like we use fopen() fread() accordingly to read in C++ and PHP( better: file_get_contents() ).
Any help or example?
oh no


Simply and sadly, no, its sad to see that there are no built in libraries neither are there modules to load/write/save and close a file either its txt or xml or ini.

The current ini plugin is broken.


Quote from: Doom_Kill3R on Dec 08, 2015, 09:45 AMThe current ini plugin is broken.
I never used the ini plugin tho.

Anyways, thanks.

Btw, what does the function file() does?( saw it on Rulk's sunshine script ).
I can use that to write data, not to see :(
oh no


The Squirrel standard library offers IO functionality. It's just a little more complicated for new users that don't know to process files in binary mode.

To open a file we use the file type constructor and pass the file name and desired flags/modes that we want to use when opening that file. Flags/modes are the similar to the C modes from fopen.

local myfile = file("text.txt","rb+");
In this case 'b' tells us that we open the file on binary mode and 'r+' allows us to both read and write/update to the file. The file must exist!

Then we use the readn(type); member function to read each byte from that file.

local my_str = "";

while (!myfile.eos())
    my_str += format(@"%c", myfile.readn('b'));


We begin by creating a string variable `my_str` where we store the resulted string. Then we use the `eos()` (end of stream) member function to know when we reached the end of the file. If we reached the end of the file then `eos()` will return something different from null.

Then we read a single byte and since ASCII characters occupy a single byte that means we read a single character from the file. But a byte is nothing but a number. So we use the format function to convert that byte into a character which becomes a string. And we add the returned string which contains a single character to the actual string.

The list of values that you can read are as follows:
  • 'i' 32bits number returns an integer
  • 's' 16bits signed integer returns an integer
  • 'w' 16bits unsigned integer returns an integer
  • 'c' 8bits signed integer returns an integer
  • 'b' 8bits unsigned integer returns an integer
  • 'f' 32bits float returns an float
  • 'd' 64bits float returns an float

After that we print the retrieved text from the file. To write to a file we use the writen(type); member function which works just like the read function except it writes values.

local my_text = "Appended text goes here";, 'e');

foreach (c in my_text)
    myfile.writen(c, 'b');

We begin by creating a dummy text string that will be appended to our current text. Then we move the cursor to the end of the file. We do that by seeking 0 bytes past the endpoint.

Then we use a foreach loop to process each character in our string. And since one ASCII character is one byte then we write them each as one byte.

The list of values that you can write are as follows:

  • 'l' processor dependent, 32bits on 32bits processors, 64bits on 64bits prcessors
  • returns an integer 'i'
  • 32bits number 's'
  • 16bits signed integer 'w'
  • 16bits unsigned integer 'c'
  • 8bits signed integer 'b'
  • 8bits unsigned integer 'f'
  • 32bits float 'd'
  • 64bits float

After we're done with the file we use the `close()` member function to close it and that's it.


I suppose the whole code can be summarized into two simple read/write functions:
function ReadTextFromFile(path)
    local f = file(path,"rb"), s = "";

    while (!f.eos())
        s += format(@"%c", f.readn('b'));


    return s;

function WriteTextToFile(path, text)
    local f = file(path,"rb+"), s = "";, 'e');

    foreach (c in text)
        f.writen(c, 'b');


And used as:
print( ReadTextFromFile("test.txt") );

WriteTextToFile("test.txt", "Appended text goes here");

print( ReadTextFromFile("test.txt") );

I suppose the whole read function can also be optimized with a blob to avoid a ton of IO operations for a single byte.

function ReadTextFromFile(path)
    local f = file(path,"rb"), s = "", n = 0;, 'e');
    n = f.tell();
    if (n == 0)
        return s;, 'b');
    local b = f.readblob(n+1);
    for (local i = 0; i < n; ++i)
        s += format(@"%c", b.readn('b'));
    return s;


Thanks a lot SLC! That helped me a lot.
Locked and marked as solved.
@Doom_Killer you may find this useful too.
oh no