Jump to content

StringInStr and FileSetPos


Recommended Posts

Hello!

I have a problem with using results of StringInStr() function in FileSetPos() function. I want to find substring "</Certificate>" from file "source.txt" (attached), move pos and read after.

Tried by using this code (see comments):

#include <FileConstants.au3>
$hFile = FileOpen("source.txt",0)
$data = FileRead($hFile)
$data = StringReplace($data,@CRLF,""); to make one line string
$data = StringReplace($data,@CR,""); to make one line string
$data = StringReplace($data,@LF,""); to make one line string
$position = StringInStr($data,"</Certificate>",0); Result = 6067
ConsoleWrite($position & @CRLF); 6067
FileSetPos($hFile, $position, $FILE_BEGIN); move read position 0 to 6067
ConsoleWrite(FileGetPos($hFile)); 6067
$data = FileRead($hFile)
$data = StringReplace($data,@CRLF,""); to make one line string
$data = StringReplace($data,@CR,""); to make one line string
$data = StringReplace($data,@LF,""); to make one line string
MsgBox(0,"New Cert text",$data); <--- WRONG TEXT! It's not 6067! It's 6061!
FileClose($hFile)

What's wrong? Thanks :)

source.txt

Edited by ABSOLUTE
Link to comment
Share on other sites

Good evening @ABSOLUTE, and welcome to the AutoIt forum :)

I couldn't run your script at the moment, but, I'd like to let you understand what you did...

To do so, I'll comment your code :)8

#include <FileConstants.au3>
$hFile = FileOpen("source.txt",0) ; Open the file
$data = FileRead($hFile) ; Read the file content
$data = StringReplace($data,@CRLF,""); to make one line string ( Replacing all the @CRLF with space )
$data = StringReplace($data,@CR,""); to make one line string ( Same )
$data = StringReplace($data,@LF,""); to make one line string ( Same )
; Retrieve the position of the string
$position = StringInStr($data,"</Certificate>",0); Result = 6067
ConsoleWrite($position & @CRLF); 6067
FileSetPos($hFile, $position, $FILE_BEGIN); move read position from 0 to 6067
ConsoleWrite(FileGetPos($hFile)); 6067
$data = FileRead($hFile) ; Read again the file content... But, where is all the part with StringReplace($data, @CRLF, " "), and so on? 
MsgBox(0,"New Cert text",$data); <--- WRONG TEXT! It's not 6067! It's 6061!
FileClose($hFile)

Did you try to do same sobstitutes after "re-reading" the content of the file into the variable $data? :)

 

Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Developers

The file is opened for reading. Look at the helpfile for FileSetPos() for the appropriate setting in the example!

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

20 minutes ago, Jos said:

The file is opened for reading. Look at the helpfile for FileSetPos() for the appropriate setting in the example!

Jos

In other words, I can't use this function for reading only?

Link to comment
Share on other sites

  • Developers

I guess you are confused and expect the StringReplace() function, which remove the @cr & @lf characters to be rewritten to the file.... right?
You then read the file again, which of course will retrieve the same text as the first read operation as you did not write the information back.
That is why i mentioned to look at the example as that does what you are possibly looking for.
So ... what is it you want to accomplish here?

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Try these two one liners MsgBox's, and, your example script of post #1 modified with comments..

MsgBox(0, 'All text following "</Certificate>" in "source.txt" file', StringRegExpReplace(FileRead("source.txt"), "(?s)^(.*\Q</Certificate>\E)", ""))
MsgBox(0, 'All text following and including "</Certificate>" in "source.txt" file', StringRegExpReplace(FileRead("source.txt"), "(?s)^.+(?=\Q</Certificate>\E)", ""))

#include <FileConstants.au3>
$hFile = FileOpen("source.txt", 0) ; Open the file
$data = FileRead($hFile) ; Read the file content

; Note: Vertical whitespace characters ae counted in StringInStr and FileSetPos functions.
; Also, you are not replacing all the @CRLF with space, you are replacing all the @CRLF with nothing (you are deleting all the @CRLF's).
;$data = StringReplace($data,@CRLF,""); to make one line string ( Replacing all the @CRLF with space )
;$data = StringReplace($data,@CR,""); to make one line string ( Same )
;$data = StringReplace($data,@LF,""); to make one line string ( Same )

; Note: StringInStr returns position 1-based; FileSetPos position is 0-based.
$position = StringInStr($data, "</Certificate>", 0) - 1 ; Minus 1 to convert the StringInStr position to FileSetPos positiom.
ConsoleWrite($position & @CRLF) ;

FileSetPos($hFile, $position, $FILE_BEGIN) ; move read position from 0 to 6067
ConsoleWrite(FileGetPos($hFile)) ; 6067
$data = FileRead($hFile) ; Read again the file content... But, where is all the part with StringReplace($data, @CRLF, " "), and so on?
MsgBox(0, "New Cert text", $data) ; <--- WRONG TEXT! It's not 6067! It's 6061!
FileClose($hFile)

 

Link to comment
Share on other sites

The file contains invalid UTF8 data, so no surprise things turn bad.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

9 hours ago, Malkey said:

Try these two one liners MsgBox's, and, your example script of post #1 modified with comments..

Not working :( First and Second message boxes shows:

<Certificate>....many text here....<IssueDate
</Certificate>....many text here....<IssueDate

Third message box shows:

SerialNumber/>...many text here...<IssueDate

 

6 hours ago, jchd said:

The file contains invalid UTF8 data, so no surprise things turn bad.

The file contains valid data, but in the Cyrillic

 

19 hours ago, Jos said:

I guess you are confused and expect the StringReplace() function, which remove the @cr & @lf characters to be rewritten to the file.... right?
You then read the file again, which of course will retrieve the same text as the first read operation as you did not write the information back.
That is why i mentioned to look at the example as that does what you are possibly looking for.
So ... what is it you want to accomplish here?

Jos 

Ouch..Really, this is so.

I will try harder. Thanks!

Link to comment
Share on other sites

7 hours ago, ABSOLUTE said:

The file contains valid data, but in the Cyrillic

Look at the file header of this XML file: it (correctly) declares UTF8 encoding but many strings are invalid UTF8 and probably use instead some cyrillic codepage.
At the end of the day the document is inconsistent as it is.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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