SA-MP Forums Archive
"RemoveSingleLineFromFile" doesn't really work - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: "RemoveSingleLineFromFile" doesn't really work (/showthread.php?tid=220859)



"RemoveSingleLineFromFile" doesn't really work - Kwarde - 04.02.2011

Hi,

My "RemoveSingleLineFromFile" isn't working. The problem is in the 'while fread' stuff, but idk why.
Here's the function:

pawn Код:
#define func:%0(%1) stock %0(%1)

func:RemoveSingleLineFromFile(file[], searchfor[], bool:useuppercase = false)
{
    new File:fileFile, File:tempFile,
        str1[35], str2[100]
    ;
   
    format(str1, 35, "Temp/TempFile%s.tmp", file);
   
    tempFile = fopen(str1, io_append);
    fileFile = fopen(file, io_read);
   
    while(fread(fileFile, str2)){
        if(useuppercase){
            if(!strcmp(str2, searchfor, false)){
                fwrite(tempFile, str2);
                    break;
            }
        }
        else{
            if(!strcmp(str2, searchfor)){
                fwrite(tempFile, str2);
                        break;
            }
        }
    }
   
    fclose(tempFile);
    fclose(fileFile);
   
    tempFile = fopen(str1, io_read);
    fileFile = fopen(file, io_write);
    fwrite(fileFile, " ");
    fclose(fileFile);
    fileFile = fopen(file, io_append);
   
    while(fread(tempFile, str2))
        fwrite(fileFile, str2);
       
    fclose(fileFile);
    fclose(tempFile);
    fremove(str1);
    return 1;
}
Does anyone knows what's wrong? Because I don't :P


Re: "RemoveSingleLineFromFile" doesn't really work - dice7 - 04.02.2011

Do you have a folder called "Temp" in your scriptfiles ?


Re: "RemoveSingleLineFromFile" doesn't really work - Finn - 04.02.2011

Why do you even need a folder for a temporary file?

Try this code. Maybe works, maybe doesn't:

pawn Код:
RemoveSingleLineFromFile(filename[], searchfor[], bool:useuppercase = false)
{
    new File:file = fopen(filename, io_read),
        File:tempfile = fopen("temp.txt", io_append),
        string[128];

    while(fread(file, string))
    {
        if(!strcmp(string, searchfor, !(useuppercase)))
        {
            continue;
        }

        fwrite(tempfile, string);
    }

    fclose(tempfile);
    fclose(file);
    fremove(filename);

    tempfile = fopen("temp.txt", io_read);
    file = fopen(filename, io_append);

    while(fread(tempfile, string))
    {
        fwrite(file, string);
    }

    fclose(tempfile);
    fclose(file);
    fremove("temp.txt");
    return 1;
}



Re: "RemoveSingleLineFromFile" doesn't really work - [03]Garsino - 04.02.2011

Quote:
Originally Posted by ******
Посмотреть сообщение
Why would it not work? You've posted it on the forums in response to a question, so that means you have previously tested it. Given that you posted it that means your tests succeeded, so if the code works for you, why would it not work for anyone else?

Also, you both know you can use "ftemp" instead of "fopen" to get a temporary file right?
ftemp? I didn't know a function like that existed. The WIKI article is a stub so there is no info available?


Re: "RemoveSingleLineFromFile" doesn't really work - Kwarde - 04.02.2011

First of all; I'm not stupid so I have the Temp folder
And Finn, I'll try it out xD


Re: "RemoveSingleLineFromFile" doesn't really work - TheAlienForce - 04.02.2011

Making a ini reader/write heey
xD


Re: "RemoveSingleLineFromFile" doesn't really work - Kwarde - 04.02.2011

No I'm not :P - I wanted to use it for my vehicle system. But I think I'm just gonna use djson.
However why should I make an ini reader/writer? (ps I wouldn't make an 'ini' system, just file writer/reader blabla). There're already enough of it xD

However,
Tomorrow I'll have time to test it