SA-MP Forums Archive
How to get all of fread to one string? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to get all of fread to one string? (/showthread.php?tid=490618)



How to get all of fread to one string? - Riwerry - 27.01.2014

I have of problem with checking the string (which is full file) with strlen(string), becouse it checks only the last fread see down:

Thx.

pawn Код:
YCMD:test (playerid, params [], help)
{
    if (fexist ("Skins.txt"))
    {
        new File:File = fopen ("Skins.txt", io_read), string [50]; // <--- THIS STRING
        fread(File, string);
        fread(File, string);
        fread(File, string); //ONLY CHECKS THIS ONE
        fclose(File);

        if (strlen(string))
        {
            SendClientMessage (playerid, -1, "File is written already");
        }
        else SendClientMessage (playerid, -1, "File is empty!");
       
    }
    return true;
}



AW: How to get all of fread to one string? - NaS - 27.01.2014

Do this:

Код:
//fopen etc..

while(fread(File, string))
{

}

//fclose etc..
The code inside the while will only be executed if there was something read from the file


Re: How to get all of fread to one string? - Riwerry - 27.01.2014

pawn Код:
while(fread, File, string))
{

}
What to put here?


AW: How to get all of fread to one string? - NaS - 27.01.2014

Inside the while you can put what you want to do with the string.

"string" will always contain the line which was read, so it depends what you wanna do with it. Or did I get something wrong?

EDIT: Oh, do you want to have every line that is in that file into ONE single line?


Re: How to get all of fread to one string? - Riwerry - 27.01.2014

Hmm, and where then I can add else statement or something when file is empty? (Cuz I will use fremove)


AW: How to get all of fread to one string? - NaS - 27.01.2014

This is one possibility how to count the lines quite simple:

Код:
fopen(..);

new LineCount = 0;
while(fread, File, string))
{
LineCount ++;
}

fclose(File); // Close it before removing!

if(LineCount == 0) 
{
// No lines found (empty file), remove the file..
fremove(..);
}



Re: How to get all of fread to one string? - Riwerry - 27.01.2014

Hmm but it will be littlebit harder for me becouse is possible so I can read random integer (number) from one line and then I delete it? :c

(Usage: reservarting skins)


Re: How to get all of fread to one string? - [ABK]Antonio - 27.01.2014

Quote:
Originally Posted by Riwerry
Посмотреть сообщение
Hmm but it will be littlebit harder for me becouse is possible so I can read random integer (number) from one line and then I delete it? :c

(Usage: reservarting skins)
What are you trying to do exactly?


Re: How to get all of fread to one string? - Riwerry - 27.01.2014

When is player creating character (<-- Already done), it will set his skin to the random one from the .txt file. Then this random line will be deleted from .txt file. On each line will be numbers, which is skinid. The reason, why I want to check, if file is empty is becouse when it got empty (all skinid used) I will rewrite it back..


AW: How to get all of fread to one string? - NaS - 27.01.2014

Do you want to reservate the skins for single players, or for admins/factions?

If it's for groups, an array would be the most simple and easy method.
You don't need to manipulate a file several times, just load/save when initting/exitting.

Do you know how arrays work?