Jump to content

Add string to big file


Recommended Posts

Hi!

I'm working on an uploader. It uploads files throu PHP. For this I have to read a file and add some string to it. But if that file is too big, then it exits with this message: "Error allocating memory".

AU3 Snippet to illustrate my prob:

$fFile = "100MBfile.bin"        ;Contains 0x00 -> i have to use raw mode
$hFile = FileOpen($fFile, 4)    ;Open raw mode
$sFile = FileRead($hFile, FileGetSize($fFile))  ;Reads properly
FileWrite("output.txt", "StringBegin"&@CRLF& _
                        $sFile&@CRLF& _
                        'StringEnd'&@CRLF   ;Fails with allocating memory error
FileClose($hFile)

It reads the file properly, but if I add some string to it then it fails.

If you run this code with a file thats big enough then you will get the same message.

How can I add a string to big files without modifying the file itself?

Edited by Izebize
Link to comment
Share on other sites

Simply use FileWrite with a handle

$handle = Fileopen("file",2)
FileWrite($handle,"Text")
FileClose($handle)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Malkey's right. You have to read what you want, close the file, then open it again in write mode. I think you want to add strings to the beginning and end of the text, not replace it. Try this:

$file = "Test.txt"
 $OpenRead = FileOpen($file, 0)
 $Read = FileRead($OpenRead, FileGetSize($file))
 
 FileClose($OpenRead)
 FileWrite("output.txt", "StringBegin" & @CRLF & $Read & @CRLF & "StringEnd")
Edited by dantay9
Link to comment
Share on other sites

You can see in my snippet, that I just read the file. I use write on "output.txt".

I could use ConsoleWrite, or MsgBox instead of FileWrite, but if you run it in Scite like most of us do, then your computer would not response to anything for a couple of days :)

Edited by Izebize
Link to comment
Share on other sites

According to the help file FileWrite does "append a line of text to the end of a previously opened text file."

So simply open the file, write the record (it's automtically appended) and close the file.

Please use a handle (means: use fileopen) because "For parsing large text files this will be much slower than using filehandles."

As you want to append to a new file I would use:

filecopy("source","target",9)          ; Create a copy of the source file (replace existing file and create directory structure if missing
$handle = Fileopen("target",2)         ; Open the target file
FileWrite($handle,"Text")              ; Append text to the target file
FileClose($handle)                     ; Close target file
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

I've also read the help file.

I have to add string before, and after the file.

I also have to use raw mode, because it contains 0x00 char, and if i open it a regular way it stops after 4 chars, because of the 0x00 char (null-terminated string).

As I said before, I can read the file. The only problem is I can't add any string to it.

Edited by Izebize
Link to comment
Share on other sites

  • Moderators

I've also read the help file.

I have to add string before, and after the file.

I also have to use raw mode, because it contains 0x00 char, and if i open it a regular way it stops after 4 chars, because of the 0x00 char (null-terminated string).

As I said before, I can read the file. The only problem is I can't add any string to it.

Take a look at:

_WinAPI_CreateFile()

_WinAPI_WriteFile()

In the help file. WriteFile has the option to insert data at specific points of the file without having to read it all.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I think I understand your problem :)

Haven't tested it but I would try something like:

FileWrite("front.txt","line of text")
FileWrite("end.txt","line of text")
RunWait(@ComSpec & " /c " & "copy front.txt /B + 10mb.txt /B + end.txt /B output.txt /B")

This should copy the 3 input files to the output file in binary format.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

@SmOke_N: Thanks! It looks good, I'll check it right now.

@water: That probably works, but with this method I have to copy the big file once more every time, and that takes too much time if I want to work with hundreds of big files.

Link to comment
Share on other sites

Finally I got the solution.

I used DllStruct like this:

$fFile = "100MBfile.bin"        ;Contains 0x00 -> i have to use raw mode
$hFile = FileOpen($fFile, 4)    ;Open raw mode
$sStringB = "StringBEGIN"
$sStringE = "StringEND"
$tBuffer = DllStructCreate("byte["&StringLen($sStringB)&"];byte["&FileGetSize($fFile)&"];byte["&StringLen($sStringE)&"]")
DllStructSetData($tBuffer, 1, $sStringB)
DllStructSetData($tBuffer, 2, FileRead($hFile, FileGetSize($fFile)))
DllStructSetData($tBuffer, 3, $sStringE)
FileClose($hFile)
FileWrite($fNew, DllStructGetData($tBuffer, 1) & _
                DllStructGetData($tBuffer, 2) & _
                DllStructGetData($tBuffer, 3))
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...