Modules/FileSystem/file/readShort: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two. ==Syntax== <syntaxhighlight lang="lua"> short file:readShort () </syntaxhighlight> ==Returns== Returns the short integer if it was successfully read from the file, '''false''' otherwise. ==Example== <section name="Client" class="client" show="true"> This snippet reads and writes string buffers into a custom file format. <syntaxhighlight la...")
 
No edit summary
 
Line 1: Line 1:
<pageclass class="#3cc882" subcaption="File function"></pageclass>
__NOTOC__
__NOTOC__
This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two.
This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two.

Latest revision as of 03:01, 23 January 2022

This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two.

Syntax

short file:readShort ()

Returns

Returns the short integer if it was successfully read from the file, false otherwise.

Example

Click to collapse [-]
Client

This snippet reads and writes string buffers into a custom file format.

local function writeBuffers( outFile, buffers )
    -- Write header information.
    outFile:writeInt( #buffers ); -- number of buffers
    
    -- Write the buffers.
    for m,n in ipairs( buffers ) do
        local bufferContent = tostring( n );

        -- Write header of buffer.
        outFile:writeShort( #bufferContent ); -- length of buffer

        -- Write the content
        outFile:write( bufferContent );
    end
end

local function readBuffers( inFile )
    local buffers = {};

    -- Read header information.
    local numBuffers = inFile:readInt();

    -- Make sure we read the int correctly.
    -- Fails if the file has less than 4 bytes to read.
    if ( numBuffers ) then
        local n = 1;

        while ( n <= numBuffers ) do
            -- Read buffer header.
            local bufferSize = inFile:readShort();

            -- We always have to add the amount of buffers specified by the header.
            -- Hence we always add a buffer to the result array.
            local buffer = "";

            if ( bufferSize ) then
                -- Read the buffer.
                buffer = inFile:read( bufferSize );
            end

            -- Append the result buffer.
            buffers[n] = buffer;

            n = n + 1;
        end
    end

    return buffers;
end

-- Manager some buffers in a file.
local myBuffers = { "Hello World!", "Cooking eggs on a frying pan", "The American Pizzas taste awesome", "There is no bathroom." };

local theFile = fileCreate( "buffers.dat" );

-- First write the buffers into the file.
writeBuffers( theFile, myBuffers );

-- Now retrieve them again.
theFile:seek( 0, "set" );

myBuffers = readBuffers( theFile );

-- Output the buffers.
for m,n in ipairs( myBuffers ) do
    outputChatBox( n );
end

FileSystem File Functions