Jump to content

Reading file returns number instead of string


Recommended Posts

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

 

Link to comment
Share on other sites

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. ;-)

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...