Jump to content

Recommended Posts

Posted

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

  • Developers
Posted

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 )

Parameters

filename 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.
  :)

Posted

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
Posted

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

Posted

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
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...