Izebize Posted July 14, 2009 Share Posted July 14, 2009 (edited) 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 July 14, 2009 by Izebize Link to comment Share on other sites More sharing options...
Malkey Posted July 14, 2009 Share Posted July 14, 2009 A file can only be in either read or write mode; it cannot be in both. Link to comment Share on other sites More sharing options...
water Posted July 14, 2009 Share Posted July 14, 2009 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
dantay9 Posted July 14, 2009 Share Posted July 14, 2009 (edited) 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 July 14, 2009 by dantay9 Link to comment Share on other sites More sharing options...
Izebize Posted July 14, 2009 Author Share Posted July 14, 2009 (edited) 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 July 14, 2009 by Izebize Link to comment Share on other sites More sharing options...
water Posted July 14, 2009 Share Posted July 14, 2009 (edited) 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 July 14, 2009 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Izebize Posted July 14, 2009 Author Share Posted July 14, 2009 (edited) 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 July 14, 2009 by Izebize Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted July 14, 2009 Moderators Share Posted July 14, 2009 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 More sharing options...
water Posted July 14, 2009 Share Posted July 14, 2009 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Izebize Posted July 14, 2009 Author Share Posted July 14, 2009 @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 More sharing options...
Izebize Posted July 15, 2009 Author Share Posted July 15, 2009 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 More sharing options...
trancexx Posted July 16, 2009 Share Posted July 16, 2009 Wow! You are on completely different level. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now