AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading a file with new system. (https://forums.alliedmods.net/showthread.php?t=299012)

eyal282 06-29-2017 14:43

Reading a file with new system.
 
HamletEagle told me using read_file is a bad idea and I tried using fopen instead. The only tutorial does not work for me and I am trying to read a file from end to start.

Can someone please show me a code of an example or really specific directions?

klippy 06-29-2017 16:03

Re: Reading a file with new system.
 
Why would you read the file from end to start? Give me a valid reason.

eyal282 06-29-2017 17:35

Re: Reading a file with new system.
 
Quote:

Originally Posted by KliPPy (Post 2532527)
Why would you read the file from end to start? Give me a valid reason.

I want to delete lines by overwriting and if I start from end, it's done easier without annoying calculations.

klippy 06-29-2017 18:29

Re: Reading a file with new system.
 
Files can be read only forwards, not backwards. Better describe what exactly you are trying to do together with examples.

Bugsy 06-29-2017 20:29

Re: Reading a file with new system.
 
Quote:

Originally Posted by eyal282 (Post 2532537)
I want to delete lines by overwriting and if I start from end, it's done easier without annoying calculations.

Just read from the start, write lines that you want to a new file and skip writing those that you want to be deleted. Delete original file and rename new file to old filename.

eyal282 06-30-2017 06:55

Re: Reading a file with new system.
 
Quote:

Originally Posted by Bugsy (Post 2532559)
Just read from the start, write lines that you want to a new file and skip writing those that you want to be deleted. Delete original file and rename new file to old filename.

When I use do client_print in every line with ftell ( debug ), it shows:
Code:

3
6
9
12
15...

Why does it jump?

klippy 06-30-2017 07:55

Re: Reading a file with new system.
 
Can't really help you unless you post a file that you're reading and the code that reads the mentioned file.
Also, ftell() doesn't return the line number. From http://www.cplusplus.com/reference/cstdio/ftell/:
Quote:

For binary streams, this is the number of bytes from the beginning of the file.

For text streams, the numerical value may not be meaningful but can still be used to restore the position to the same position later using fseek (if there are characters put back using ungetc still pending of being read, the behavior is undefined)

eyal282 06-30-2017 09:50

Re: Reading a file with new system.
 
Now it works, I used a variable instead and it is probably better than natives.
PHP Code:

    new NextLine Lines[0];
    new 
CurrentLine;
    while(
fgets(FilePointerTextcharsmax(Text)))
    {            
        
CurrentLine++;
        
client_print(0print_chat"Current Line: %i"CurrentLine);
        if( ( 
ftell(FilePointer) == NextLine && !StripSpaces ) || ( strcmp(Text"") == && StripSpaces ) )
        {
            if(
StripSpaces || ( !StripSpaces && LinesLength != LinesDestroyed ) )
            {
                
LinesDestroyed++;
                
NextLine Lines[LinesDestroyed];
                continue;
            }
        }    
        
        
fputs(TempFilePointerText);

    } 

For some reason, empty lines are not captured. I tried strcmp(Text, "") == 0, !Text[0] and no luck, any reason?

klippy 06-30-2017 11:33

Re: Reading a file with new system.
 
Because they probably contain "\n" or "\n\r". Do trim() on the string. Also you can check if the string is empty by doing
PHP Code:

if(Text[0] == 0


eyal282 06-30-2017 13:43

Re: Reading a file with new system.
 
Quote:

Originally Posted by KliPPy (Post 2532671)
Because they probably contain "\n" or "\n\r". Do trim() on the string. Also you can check if the string is empty by doing
PHP Code:

if(Text[0] == 0


Works flawlessly, +karma if that's how karma works.

Note: Your check is equal to mine ( !Text[0] ), what helped was trim(Text);


All times are GMT -4. The time now is 23:01.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.