stojko22

Reading file returns number instead of string

8 posts in this topic

#1 ·  Posted

Hi,

i'm trying to read a big ini file so ini read don't work for me. Now i use file read but i getting back only a numbers not a results with string that match search term, how i can fix that ?

 

#include <FileConstants.au3>

$file = FileOpen("test.ini") ; open file

$bb = FileRead($file) ; read file

If @error Then
    ConsoleWrite("Error : " & @error & @CRLF)
Else
    $aa = StringInStr($bb, "Blode") ; find in ini file

    If @error Then
        ConsoleWrite("Error : " & @error & @CRLF)
    Else
        ConsoleWrite($aa) ; returns 6240146
    EndIf
EndIf

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

Quote

StringInStr

Checks if a string contains a given substring.

StringInStr ( "string", "substring" [, casesense = 0 [, occurrence = 1 [, start = 1 [, count]]]] )

Parameters

string The string to evaluate.
substring The substring to search for.
casesense [optional] Flag to indicate if the operations should be case sensitive.
    $STR_NOCASESENSE (0) = not case sensitive, using the user's locale (default)
    $STR_CASESENSE (1) = case sensitive
    $STR_NOCASESENSEBASIC (2) = not case sensitive, using a basic/faster comparison

Constants are defined in StringConstants.au3.
occurrence [optional] Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side. The default value is 1 (finds first occurrence).
start [optional] The starting position of the search.
count [optional] The number of characters to search. This effectively limits the search to a portion of the full string. See remarks.

Return Value

Success: the position of the substring.

StringInStr returned the position where the substring was found, so returning a number is a good thing. ;-)

Share this post


Link to post
Share on other sites

#4 ·  Posted

3 hours ago, Floops said:

Something like this should work

 

$file = FileOpen("test.ini") ; open file

$i = 0
Do
    $i += 1
    $bb = FileReadLine($file, $i)
    $iError = @error

    If StringInStr($bb, "Blode") Then ConsoleWrite("Substring found in line: " & $i & " | Text: " & $bb & @CRLF)

Until $iError = -1

FileClose($file)

 

I try this but works slow, it found result in first few lines fast, but i have almost million lines and this way need very much time to find a string. 

Share this post


Link to post
Share on other sites

#5 ·  Posted

it's because of the use of the line number parameter in filereadline. This leads to reiteration from file beginning every time the function is called.
Remove the second parameter and it should run significantly faster:

$file = FileOpen("test.ini") ; open file

$i = 0
Do
    $i += 1
    $bb = FileReadLine($file)
    If @error Then ExitLoop

    If StringInStr($bb, "Blode") Then ConsoleWrite("Substring found in line: " & $i & " | Text: " & $bb & @CRLF)

Until False

FileClose($file)

 

2 people like this

Share this post


Link to post
Share on other sites

#6 ·  Posted

2 minutes ago, AspirinJunkie said:

it's because of the use of the line number parameter in filereadline. This leads to reiteration from file beginning every time the function is called.
Remove the second parameter and it should run significantly faster:

$file = FileOpen("test.ini") ; open file

$i = 0
Do
    $i += 1
    $bb = FileReadLine($file)
    If @error Then ExitLoop

    If StringInStr($bb, "Blode") Then ConsoleWrite("Substring found in line: " & $i & " | Text: " & $bb & @CRLF)

Until False

FileClose($file)

 

Thanks, this works now like a charm :)

Share this post


Link to post
Share on other sites

#7 ·  Posted

14 minutes ago, AspirinJunkie said:

it's because of the use of the line number parameter in filereadline. This leads to reiteration from file beginning every time the function is called.

Damn I never knew of this until now. I should really pay more attention to the help files.. :sweating:

Share this post


Link to post
Share on other sites

#8 ·  Posted

i'm trying to read a big ini file so ini read don't work for me

Why??? Autoit have a Function to do it and work as well, Use IniReadSectionNames or IniReadSection and use a loop to find your value.

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now