Fread

From SA-MP Wiki

Jump to: navigation, search


Read from a file.


Parameters:
(File:handle, string[], size = sizeof string, bool: pack = false)
handleThe file to read from
stringThe string to store the read text in, passed by reference
sizeThe number of bytes to read
packShould the string reference be packed?


ReturnsThe length of string (the read text) as an integer.


public OnGameModeInit()
{
    new string[64]; // Create the string to store the read text in
    new File:example = fopen("Startup.txt", io_read); // Open the file
    fread(example, string); // Fread from the file and store what's read in 'string'
    fclose(example); // Close the file
    printf("%s",string); // Print what was read
    return 1;
}


Reading Line-by-Line

public OnPlayerConnect(playerid)
{
    new string[64]; // Create the string to store the read text in
    new File:example = fopen("Startup.txt", io_read); // Open the file
    while(fread(example, string)) //reads the file line-by-line
    {
        if(strcmp(string, "Ban", true) == 0) //if any of the lines in the file say "Ban" the system will ban the player
        {
            Ban(playerid); //bans the player
        }
    }
    fclose(example);
    return 1;
}

Related Functions

The following functions may be useful, as they are related to this function in one way or another.

Personal tools