Jump to content

FileRead doesn't read from the start of the file per variable


 Share

Recommended Posts

Hello everyone

Is the result I'm getting, normal?  Here's my code:

$tbxfileopen = FileOpen ("IATE_export_29082018.tbx", 16) ; a 2 GB text file

$testfileopen0 = FileOpen ("test0.txt", 17)
$testfileopen1 = FileOpen ("test1.txt", 17)

$tbxfileread0 = FileRead ($tbxfileopen, 1000000)
$tbxfileread1 = FileRead ($tbxfileopen, 1000000)

FileWrite ($testfileopen0, $tbxfileread0)
FileWrite ($testfileopen1, $tbxfileread1)

I would have expected "test0.txt" and "test1.txt" to have the same content, but what I find is that "test1.txt" starts where "test0.txt" ends, despite the fact that the "FileRead" is being read into two separate variables.  Is this supposed to happen like that?  Why?  And, how can I avoid that... (except by using "FileOpen" twice).  I mean, surely FileRead always reads from the start of the file, so for each new FileRead instance, wouldn't it have to start from the start of the file again?

Thanks

Samuel

Link to comment
Share on other sites

Yes, it's OK.

Every FileRead() moves internal "current file position" for that opened file handle. 

Use FileSetPos() to move position to 0 before second FileRead() to achieve reading from the begin of file.

$tbxfileread0 = FileRead ($tbxfileopen, 1000000)
FileSetPos($tbxfileopen, 0, $FILE_BEGIN)
$tbxfileread1 = FileRead ($tbxfileopen, 1000000)

 

Look here

https://www.autoitscript.com/autoit3/docs/functions/FileSetPos.htm

 

Edited by Zedna
Link to comment
Share on other sites

2 hours ago, leuce said:

despite the fact that the "FileRead" is being read into two separate variables.

FileRead has no idea what variables, if even any, it’s return value may be going in to.  Therefore it can’t make decisions based on such.

Code hard, but don’t hard code...

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