Raised This Month: $ Target: $400
 0% 

Reading a file with new system.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-29-2017 , 14:43   Reading a file with new system.
Reply With Quote #1

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?
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-29-2017 , 16:03   Re: Reading a file with new system.
Reply With Quote #2

Why would you read the file from end to start? Give me a valid reason.
klippy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-29-2017 , 17:35   Re: Reading a file with new system.
Reply With Quote #3

Quote:
Originally Posted by KliPPy View Post
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.
eyal282 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-29-2017 , 20:29   Re: Reading a file with new system.
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
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.
__________________
Bugsy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-30-2017 , 06:55   Re: Reading a file with new system.
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
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?
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-29-2017 , 18:29   Re: Reading a file with new system.
Reply With Quote #6

Files can be read only forwards, not backwards. Better describe what exactly you are trying to do together with examples.
klippy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-30-2017 , 07:55   Re: Reading a file with new system.
Reply With Quote #7

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)
klippy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-30-2017 , 09:50   Re: Reading a file with new system.
Reply With Quote #8

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?

Last edited by eyal282; 06-30-2017 at 10:13.
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-30-2017 , 11:33   Re: Reading a file with new system.
Reply With Quote #9

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
klippy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-30-2017 , 13:43   Re: Reading a file with new system.
Reply With Quote #10

Quote:
Originally Posted by KliPPy View Post
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);

Last edited by eyal282; 06-30-2017 at 13:44.
eyal282 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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