"RemoveSingleLineFromFile" doesn't really work
#1

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

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

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

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

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

Making a ini reader/write heey
xD
Reply
#7

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)