WhiteAvenger Posted August 26, 2008 Posted August 26, 2008 Hi, I want to open a file and read the contents either bit-by-bit or in hex. I have already tried FileRead, but that fails at reading non-text files (i.e. .exe files). This is why I want to read either the single bits or the hex characters that make up a file. Can anyone help me with that? Thanks --WhiteAvenger
BrettF Posted August 26, 2008 Posted August 26, 2008 You should be able to read a file in binary. Could you post your code please? Thanks, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
Developers Jos Posted August 26, 2008 Developers Posted August 26, 2008 Reading Bit by Bit will be a challange but Byte by Byte should work fine as long as you open the file in Binary. Opens a text file for reading or writing.FileOpen ( "filename", mode ) Parametersfilename Filename of the text file to open. mode Mode (read or write) to open the file in.Can be a combination of the following: 0 = Read mode 1 = Write mode (append to end of file) 2 = Write mode (erase previous contents) 4 = Read raw mode 8 = Create directory structure if it doesn't exist (See Remarks). 16 = Force binary(byte) reading and writing mode with FileRead and FileWrite 32 = Use Unicode UTF16 Little Endian mode when writing text with FileWrite and FileWriteLine (default is ANSI) 64 = Use Unicode UTF16 Big Endian mode when writing text with FileWrite and FileWriteLine (default is ANSI) 128 = Use Unicode UTF8 when writing text with FileWrite and FileWriteLine (default is ANSI)Both write modes will create the file if it does not already exist. The folder path must already exist (except using mode '8' - See Remarks). SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
WhiteAvenger Posted August 26, 2008 Author Posted August 26, 2008 I have already tried mode 16. But how do I get binary results? When I use mode 16, I still get normal text, rather than binary numbers... Also, how do I tell the script to read the whole file? I tried For $ct = 1 to _FileCountLines($file) FileWriteLine($writeHandle,FileReadLine($readHandle,$ct)) Next But for non-text files, _FileCountLines returned a negative integer that was the number of characters in the file + or - a number <= 10.
Developers Jos Posted August 26, 2008 Developers Posted August 26, 2008 There are no "lines" in a binary file thus should not be used that way. Just read one character at a time till you reach EOF. this can be tested by testing for @error right after FileRead(). Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
WhiteAvenger Posted August 26, 2008 Author Posted August 26, 2008 There are no "lines" in a binary file thus should not be used that way. Just read one character at a time till you reach EOF. this can be tested by testing for @error right after FileRead(). Ah, that's how to do it. I was searching for an eof() function and couldn't find one... OK, I now have the following code: #include <file.au3> $file = FileOpenDialog("open",@MyDocumentsDir,"All Files (*.*)") $readHandle = FileOpen($file,16) $writeHandle = FileOpen($file & ".cry",2) Do FileWrite($writeHandle,FileRead($readHandle,1)) Until @error FileClose($readHandle) FileClose($writeHandle) This code, however, seems to get stuck on the last few KB of each file... Do you know what I'm doing wrong?
Developers Jos Posted August 26, 2008 Developers Posted August 26, 2008 You are testing wrong. The @error contains the result of the Last performed fucntions which is FileWrite in your script, not FileRead! $file = FileOpenDialog("open", @MyDocumentsDir, "All Files (*.*)") $readHandle = FileOpen($file, 16) $writeHandle = FileOpen($file & ".cry", 16 + 2) While 1 $char = FileRead($readHandle, 1) If @error Then ExitLoop FileWrite($writeHandle, $char) WEnd FileClose($readHandle) FileClose($writeHandle) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
WhiteAvenger Posted August 26, 2008 Author Posted August 26, 2008 Oh... Now it works, thanks! What kind of result is $char? I'm opening the file in binary mode, so is $char a number? Or what exactly is it?
BrettF Posted August 27, 2008 Posted August 27, 2008 String isn't it? Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
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