Jump to content

[Solved] Error allocating memory for a 6 MB file


 Share

Recommended Posts

Hello! I am having problem with using file read...

f7683dc5a8fab1924e567f1e08b7fed1.png

Here is my code:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local Const $CHUNK = 1024
Local $hFile = FileOpen(@ScriptDir & '\test.bin', $FO_APPEND)
Local $iCurrentPos = 0

Do
    $iCurrentPos += $CHUNK
    $bFileSetPos = FileSetPos($hFile, $iCurrentPos, $FILE_END)
    $vData = FileRead($hFile)
    $vData = BinaryToString($vData)
    MsgBox(0, 0, $vData)
    $iStringPos = StringInStr($vData, "Test", $STR_NOCASESENSEBASIC)
Until Not ($iStringPos = 0) Or $bFileSetPos = False
FileClose($hFile)

MsgBox($MB_OK, "Success", $iStringPos)

Here is the 6 MB file: <Attachment Deleted>

 

Thanks in Advance! TD :D

Edited by TheDcoder
Appended [Solved] to title & deleted attachment

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

You have to define the number of bytes to read in FileRead as well. Else you read the whole file every time.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water Huh? :huh:

I remember that FileRead reads from the position set by FileSetPos, my previous version of code just did that.

Even if it did read the whole file, I think it would be easy to store 6 MB of data... This is my test script which I use to read the whole file:

ConsoleWrite(FileRead(@ScriptDir & '\test.bin'))
MsgBox(0,0,0)
1 hour ago, water said:

You have to define the number of bytes to read in FileRead as well

I tried that now, I set the number of bytes to read to $iCurrentPos, Now for some reason the script is looping indefinitely.

 

TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@water Huh? :huh:

I remember that FileRead reads from the position set by FileSetPos, my previous version of code just did that.

Even if it did read the whole file, I think it would be easy to store 6 MB of data... This is my test script which I use to read the whole file:

ConsoleWrite(FileRead(@ScriptDir & '\test.bin'))
MsgBox(0,0,0)
1 hour ago, water said:

You have to define the number of bytes to read in FileRead as well

I tried that now, I set the number of bytes to read to $iCurrentPos, Now for some reason the script is looping indefinitely.

 

TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Can you please describe what you try to do?
Now you

  • Open the file in write/append mode - but you only read the content
  • You use FileSetPos from the end of the file - do you really try to read the file from end to start?
  • You increment $iCurrentPos - but my understanding is that this should be a negative number for FileSetPos from the end of the file
  • You always read the file from the set position to the end - the longer you loop the more data you read
  • Error checking is not done when an error could occur (after FileSetPos or FileRead) - so you might process data in an inconsistant state
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

3 minutes ago, water said:

Can you please describe what you try to do?

I am trying to read the footer of a file (in this case "Test" indicates where the footer starts from) :)

5 minutes ago, water said:

Open the file in write/append mode - but you only read the content

The code is from a function in my original code, I returned the read data.

5 minutes ago, water said:

You use FileSetPos from the end of the file - do you really try to read the file from end to start?

Yep, it is necessary to read the footer :D

6 minutes ago, water said:

You increment $iCurrentPos - but my understanding is that this should be a negative number to read from the end of the file

Silly me! That fixed the problem :D

 

Thanks water! You solved my problem by asking questions! That is simply great! :thumbsup:

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

This is the code which worked for me :D:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local Const $CHUNK = 1024
Local $hFile = FileOpen(@ScriptDir & '\test.bin', $FO_APPEND)
Local $iCurrentPos = 0
Local $bFileSetPos = True

Do
    $iCurrentPos -= $CHUNK
    $bFileSetPos = FileSetPos($hFile, $iCurrentPos, $FILE_END)
    $vData = FileRead($hFile, $iCurrentPos)
    $vData = BinaryToString($vData)
    $iStringPos = StringInStr($vData, "Test", $STR_NOCASESENSEBASIC)
Until Not ($iStringPos = 0) Or $bFileSetPos = False
FileClose($hFile)

MsgBox($MB_OK, "Success", $iStringPos)

But I still don't understand why there was a memory allocation error in my faulty code :think:

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Glad the problem could be solved :) 
But I still think it is a lot of overhead to check a 6MB file ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Read the file in one go.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ah, new information :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If you need the footer only, then read the file from the end.

Edited by LarsJ
Link to comment
Share on other sites

My grandmother can code that - while she is knitting at the same time. She can multitask. You have to figure that out yourself young man.

Link to comment
Share on other sites

No, his grandmother is a woman. And women are able to multitask. At least that's what they try to tell us.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Guess what I found in the help file :)

"A count value that is too large can lead to AutoIt stopping with a memory allocation failure."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

×
×
  • Create New...