CLEO HELP Question about file reading / counting

0x688

Wtf I'm not new....
Staff member
Administrator
Joined
Feb 18, 2013
Messages
1,073
Likes
53
Points
98
#3
Which kind of file(s)?
 
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#5
The point of this is i wanna read specific line and get text.

it should work like.:
GetChatEnteryText snippet
but for .txt file(s)
 

Opcode.eXe

Well-Known Member
Joined
Feb 18, 2013
Messages
1,471
Likes
190
Points
113
Location
( ͡° ͜ʖ ͡°)
Website
www.youtube.com
#6
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#7
Last edited:
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#9
@monday
I found your old post , but when i tried use it with for [email protected] = 0 to MAX_LINES it doesn't work...
Also how to get MAX_LINES ?

PHP:
{$CLEO .cs}
0000:


repeat
wait 50
until 0AFA:  SAMP_IS_READY

alloc [email protected] 2000 // string from the new file
alloc [email protected] 2000 // string from the last file

[email protected] = true

while true
wait 0
    if [email protected] == true
    //if key_down 49
    then
    call @GetStringFromFile 2 stringPointer [email protected] debug false // the string from file will be written to the memory at address [email protected] (so there's no need for return)
        if 0C21: stricmp string1 [email protected] string2 [email protected]
        then
        //do nothing if the same string was found
        else
        chatmsg "{FFFF00}%s" -1 [email protected]
        0C13: strcpy destination [email protected] source [email protected] // copy string if the new one is found so it will be compared later
        end
    wait 700
    end
end

:GetStringFromFile
alloc [email protected] 1000
0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rb" // IF and SET
0A9C: [email protected] = file [email protected] size
0C11: memset destination [email protected] value 0 size 1000
    while 0AD7: read_string_from_file [email protected] to [email protected] size [email protected] // it reads a single line from the file and returns false if the end of file was reached
    wait 0
  
  
    /*
    this "while" loop could be replaced with for example: "for [email protected] = 0 to MAX_LINES" loop for each line in the file,
    then simple line checks could be used like:
  
    if [email protected] == 0
    then
    //[email protected] = first line
    end
  
    if [email protected] == 1
    then
    [email protected] = second line
    end
    */                                        
    0C15: strcat destination [email protected] source [email protected] // append single line ([email protected]) to the main string ([email protected]) which will include all the text within the file
    0C11: memset destination [email protected] value 0 size 1000  // clear the line before reading the next one (just in case if 0AD7 isn't doing it automatically)
    end
//0A9D: readfile [email protected] size [email protected] to [email protected] //(maybe it doesn't work with pointers - "If the number of delivered bytes is more than four, the remainder will be written to the next variable ([email protected] in this case), and so on.")
0A9B: closefile [email protected]
    if [email protected] == true
    then
    chatmsg "File contains: %s" -1 [email protected]
    end

free [email protected]
ret 0
 

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
1,022
Likes
66
Points
98
20
#11
"rt" will open it in "read text" mode, "at" stands for "append text" maybe that's why you got result equal to 1
This seems to work:
Code:
IF
0AAB:   file_exists "CLEO/test.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/test.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        alloc [email protected] 5000
        
        while 0AD7: read_string_from_file [email protected] to [email protected] size 5000 // retrieves data from a file into a buffer until it encounters a new line
        wait 0
            [email protected] += 1
        end
        
        free [email protected]
        
        0AD1: show_formatted_text_highpriority "Lines in file: %d" 15000 [email protected]
    
        0A9B: closefile [email protected]
    else
    0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
 
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#12
"rt" will open it in "read text" mode, "at" stands for "append text" maybe that's why you got result equal to 1
This seems to work:
Code:
IF
0AAB:   file_exists "CLEO/test.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/test.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        alloc [email protected] 5000
      
        while 0AD7: read_string_from_file [email protected] to [email protected] size 5000 // retrieves data from a file into a buffer until it encounters a new line
        wait 0
            [email protected] += 1
        end
      
        free [email protected]
      
        0AD1: show_formatted_text_highpriority "Lines in file: %d" 15000 [email protected]
  
        0A9B: closefile [email protected]
    else
    0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
Works, thx.
 
Last edited:
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#13
Probably this way best , to count how many lines are in file.
If i wanna get last line or one before last. like 98 or 99
PHP:
{$CLEO .cs}
0000:
wait 7000 

while true
wait 0

0AB1: @Counter 0 _ReturnTotalNumberOfLines [email protected]
0AB1: @Read_String_From_File 1 _ReadLine [email protected] _ReturnText [email protected]
chatmsg "%s" -1 [email protected]

end

:Read_String_From_File
IF
0AAB:   file_exists "CLEO/admin.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        alloc [email protected] 20000
            while 0AD7: read_string_from_file [email protected] to [email protected] size 20000 // retrieves data from a file into a buffer until it encounters a new line
            wait 0
                [email protected]++
                if
                003B:   [email protected] == [email protected]  // (int)
                then
                    0AB2: 1 [email protected]
                end
            end
        free [email protected]
        0A9B: closefile [email protected]
    else
        0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
    0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
0AB2: ret 0

:Counter
IF
0AAB:   file_exists "CLEO/admin.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        alloc [email protected] 20000
            while 0AD7: read_string_from_file [email protected] to [email protected] size 20000 // retrieves data from a file into a buffer until it encounters a new line
            wait 0
                [email protected]++ // count
            end
        free [email protected]
        0AB2: ret 1 [email protected]
        0A9B: closefile [email protected]
    else
        0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
    0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
0AB2: ret 0
 
Last edited:
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#14
Is there any way get every line nickname from .txt file without counting and doing for ?
@monday

PHP:
{$CLEO .cs}

0000:

REPEAT
WAIT 1000
0A8D: [email protected] = readMem 0xA444A0 sz 4 vp 0
UNTIL [email protected] == 1 // SAMP_IS_READY

while true
wait 0

0AB1: @Count_Lines 0 _ReturnTotalNumberOfLines [email protected]
for [email protected] = 1 to [email protected]
    0AB1: @Read_String_From_File 1 _ReadLine [email protected] _ReturnText [email protected] // return nick name from ini file
    
    0AC8: [email protected] = allocate_memory_size 145 // Chat alloc   
    0AB1: @GetChatEntryText 1 id 99 to [email protected] // get last line chat text
    
    if                 
    0AB1: call_scm_func @IfTextContains 2 mainTextPointer [email protected] pieceToFindPointer [email protected] _returnedPointer [email protected] //[email protected] is useless xDD
    then
        0AD1: "NickName was found!" 1000
    end 
    
    0AC9: free_allocated_memory [email protected]
end

end

:IfTextContains
{
    [email protected] - the main text
    [email protected] - piece of text to be found within the main text
}
0AB1: @StrLen 1 strPtr [email protected] _length [email protected]
0AB1: @StrLen 1 strPtr [email protected] _length [email protected]

//initial length check (the phrase can't be longer than the main text)
if 001D:   [email protected] > [email protected]  // (int)
then
    059A:  return_false
    0AB2: ret 1 0
end

[email protected] -= 1
[email protected] = 0 // counter of the same chars in a row
for [email protected] = 0 to [email protected] // for each char of the main text  ([email protected] = index)   
    0085: [email protected] = [email protected] // (int)
    005A: [email protected] += [email protected]  // (int)
    0A8D: [email protected] = read_memory [email protected] size 1 virtual_protect 0
    
    0085: [email protected] = [email protected] // (int)
    005A: [email protected] += [email protected]  // (int)
    0A8D: [email protected] = read_memory [email protected] size 1 virtual_protect 0

    if 003B:   [email protected] == [email protected]  // (int)   
    then
    [email protected]++
        if 002D:   [email protected] >= [email protected]  // (int)
        then
            0485:  return_true
            0062: [email protected] -= [email protected]  // (int)
            [email protected] += 1
            0AB2: ret 1 [email protected]
        end
    else
        if [email protected] > 0
        then
            [email protected] //check the same char again and assume it's the begining of the string but only if there was matching parts before (otherwise it would go back all the time and make infinite loop)
        end
    [email protected] = 0
    end           
end
 
059A:  return_false
0AB2: ret 1 0

:strlen
{
    Example: 0AB1: @strlen 1 string [email protected] return: [email protected]
    In: [email protected] - text;
    Out: [email protected] - size;
}
for [email protected] = 0 to 1024
    0A8D: [email protected] = read_memory [email protected] size 1 virtual_protect 0
    if not [email protected] == 0
    jf break
    [email protected] += 1
end
0AB2: ret 1 [email protected]

:GetChatEntryText 
IF 0AA2: [email protected] = "samp.dll"
THEN
    [email protected] += 0x21A0E4 // SAMP_CHAT_INFO_OFFSET_037
    0A8D: [email protected] readMem [email protected] sz 4 vp 0
    [email protected] += 0x132
    [email protected] *= 0xFC
    005A: [email protected] += [email protected]
    [email protected] += 0x20
    0AA3: [email protected]
END
0AB2: ret 1 [email protected]

:Read_String_From_File
IF
0AAB:   file_exists "CLEO/admin.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        0AC8: [email protected] = allocate_memory_size 20000
            while 0AD7: read_string_from_file [email protected] to [email protected] size 20000 // retrieves data from a file into a buffer until it encounters a new line
            wait 0
                [email protected] += 1
                if
                003B:   [email protected] == [email protected]  // (int)
                then
                    0AB2: 1 [email protected]
                end
            end
        0AC9: free_allocated_memory [email protected]
        0A9B: closefile [email protected]
    else
        0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
    0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
0AB2: ret 0

:Count_Lines
IF
0AAB:   file_exists "CLEO/admin.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rt"  // IF and SET
    then
        [email protected] = 0
        0AC8: [email protected] = allocate_memory_size 20000
            while 0AD7: read_string_from_file [email protected] to [email protected] size 20000 // retrieves data from a file into a buffer until it encounters a new line
            wait 0
                [email protected] += 1 // count
            end
        0AC9: free_allocated_memory [email protected]
        0AB2: ret 1 [email protected]
        0A9B: closefile [email protected]
    else
        0AD1: show_formatted_text_highpriority "Couldn't open the file" 15000
    end
else
    0AD1: show_formatted_text_highpriority "File doesn't exist" 15000
end
0AB2: ret 0
 

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
1,022
Likes
66
Points
98
20
#15
Sorry but I don't understand the question. So the file contains names right? Each line has a single name I assume. Like:
Michal
Andrzej
Janusz
And you'd like to read (and print?) all the names but without using "for" loops?

Is that correct?

Edit: I think it would be good if you specified what goal are you trying to accomplish. Is the goal to store all of the names in memory to use them later?
 
Last edited:
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#16
Sorry but I don't understand the question. So the file contains names right? Each line has a single name I assume. Like:

And you'd like to read (and print?) all the names but without using "for" loops?

Is that correct?
Yes you are right, i trying get all nick names without for, i wanna make like admin checker.
Code should get nick name from .txt file and check from chat - exist or not.
With sf is like
PHP:
{
[email protected] - chat text
[email protected] - King_Lion // nick name from .txt file
}
If
0C29: $NOT_USED  str1 [email protected]   str2 [email protected]
Then
//..
End
 
Last edited:

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
1,022
Likes
66
Points
98
20
#17
you can just read the whole file like this I think:
Code:
0A9C: [email protected] = file [email protected] size // [email protected] is received from "openfile" opcode
0085: [email protected] = [email protected]
[email protected] += 1
alloc [email protected] [email protected]  // alloc file size + 1
0A9D: readfile [email protected] size [email protected] to [email protected]
Then using strstr should work well for a text file, but keep in mind that if the file contained byte equal to 0 then the strstr would not reach the part of the file following the 0 byte.
 
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#18
you can just read the whole file like this I think:
Code:
0A9C: [email protected] = file [email protected] size // [email protected] is received from "openfile" opcode
0085: [email protected] = [email protected]
[email protected] += 1
alloc [email protected] [email protected]  // alloc file size + 1
0A9D: readfile [email protected] size [email protected] to [email protected]
Then using strstr should work well for a text file, but keep in mind that if the file contained byte equal to 0 then the strstr would not reach the part of the file following the 0 byte.
Tested , retruning NULL and after 1-2min crash.
PHP:
{$CLEO .cs}

0000:

REPEAT
WAIT 1000
0A8D: [email protected] = readMem 0xA444A0 sz 4 vp 0
UNTIL [email protected] == 1 // SAMP_IS_READY

WHILE TRUE
WAIT 0

IF
[email protected] == FALSE
THEN
    IF
    0AAB:   file_exists "CLEO\admin.txt"
    THEN
        0A9A: [email protected] = openfile "CLEO\admin.txt" mode "rt"  
        0A9C: [email protected] = file [email protected] size // [email protected] is received from "openfile" opcode
        0085: [email protected] = [email protected]
        [email protected] += 1
        0AC8: [email protected] = allocate_memory_size [email protected]                  
        0A9D: readfile [email protected] size [email protected] to [email protected]
        0AD1: "%s" 1337 [email protected]
    END
END

END
 

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
1,022
Likes
66
Points
98
20
#19
OP
OP
Parazitas

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,344
Likes
143
Points
78
Location
Lithuania
Website
ugbase.eu
15
#20
It looks like you didn't copy it properly



In this part the size parameter is incorrect, it should be the size of the file ([email protected] returned from 0A9C), the destination should be the pointer to allocated memory ([email protected]), so:
0A9D: readfile [email protected] size [email protected] to [email protected]
Same crash.

Also i tested this one , this showing all nicknames , bet when i change it to ret i get only one nickname.
PHP:
:Read_String_From_File
IF
0AAB:   file_exists "CLEO/admin.txt"
then
    if
    0A9A: [email protected] = openfile "CLEO/admin.txt" mode "rt"  // IF and SET
    then
        0AC8: [email protected] = allocate_memory_size 20000
        repeat
        wait 1000
        [email protected]++
        0AD7: read_string_from_file [email protected] to [email protected] size 20000
        chatmsg "%s" -1 [email protected]
        until 0AD6:   end_of_file [email protected] reached
        0AC9: free_allocated_memory [email protected]
        0A9B: closefile [email protected]
    end
end
0AB2: ret 0
 
Top