Jump to content
Sign in to follow this  
Izebize

Add string to big file

Recommended Posts

Izebize

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

Share this post


Link to post
Share on other sites
Malkey

A file can only be in either read or write mode; it cannot be in both.

Share this post


Link to post
Share on other sites
water

Simply use FileWrite with a handle

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
dantay9

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

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
Izebize

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

Share this post


Link to post
Share on other sites
water

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Izebize

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

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
water

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Izebize

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

Share this post


Link to post
Share on other sites
Izebize

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

Share this post


Link to post
Share on other sites
trancexx

Wow!

You are on completely different level.


♡♡♡

.

eMyvnE

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.