How to get all of fread to one string?
#1

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;
}
Reply
#2

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
Reply
#3

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

}
What to put here?
Reply
#4

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?
Reply
#5

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

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(..);
}
Reply
#7

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)
Reply
#8

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?
Reply
#9

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..
Reply
#10

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?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)