Link to home
Start Free TrialLog in
Avatar of vpcnc
vpcnc

asked on

Reverse the contents of a file

I have some file types that I need to Reverse the content.
So that the Last byte is the First byte, and the first byte is now the last byte.

Sort of like if you used a FILO buffer as you read and then resave the file byte by byte.

example if the file has this text in it...
ETARDEEF LAUNAM11 EDIRREVO ETARDEEF LAUNAM01 EDIRREVO ETARDEEF LAUNAM9 EDIRREVO ETARDDEF LAUNAM8 EDIRREVO

after it is reversed it will look like this.

OVERRIDE 8MANUAL FEDDRATE OVERRIDE 9MANUAL FEEDRATE OVERRIDE 10MANUAL FEEDRATE OVERRIDE 11MANUAL FEEDRATE

The files can be up to 32GB in size and contain binary data.

( I don't need to reverse the actual Binary bits)
If I read the first Byte of the file and it is 00010001 then I still want to write 00010001 but instead of it being the first byte, it will now be the last byte.

I could code a program to  do something like this: with 3 files...
Source file - file to be reversed
Temp File - temporary use
Destination File - results of reversal

1.) Get next Byte from Source
2.) Write Byte to Temp
3.) Get Entire content of the Destination
4.) Copy to end of Temp
5.) Dispose of Destination
6.) Copy Temp to Destination
7.) Dispose of temp

Repeat till all Bytes of the Source have been read.

When done the destination file will now be the reverse of the source file
But doing it byte by byte like this will take a long time...

I wonder if there is an Easier or Quicker way.

I have access to
VB.net VB2010 express  or VB2013 express  

I would need to be able to Select the Source File
Choose a destination File

then reverse the file.

Sample code and Logic would be very helpful.
Avatar of HooKooDooKu
HooKooDooKu

File IO sounds like a killer with that approach.  
You might as well just use two files (one input one output).  

1. Seek to the end of the input file -1.
2. Read a byte, write a byte
3. Seek to the end of the input file -2
etc

But IMHO, it would be better to do the reversal in memory.  The optimized thing would be to read the entire input file into memory, reverse the data in memory, and write the whole memory block back out.  But that approach has the obvious drawback that you likely do not have 32GB of physical memory.  (That approach might still work, but only with a lot of disk swapping at the OS level).

But with limited resources, you need a way to break the process down into smaller chunks.  So you might try to process the file in 1MB blocks.
1. Seek to the end of the file - 1MB
2. Read 1MB into memory
3. Reverse the 1MB block in memory.
4. Write the 1MB block of memory to destination file.
5. Seek to the end of the file - 2MB.
etc
Of course you have to deal with the fact that the file will not be an even number of 1MB blocks, so you'll have to come of with a way of dealing with that last <1MB block.
Avatar of vpcnc

ASKER

The Logic sounds workable
can you provide Code examples to go along with your Logic plan?

I have access to (VB.Net 2010 Prefered)  or VB.net 2013

File Selection Popup to select the file to be reversed
to select the Destination file to save the results to

Code to Seek to the end of the file and read 1MB into memory.
Code to reverse the contents of Memory
Code to Output memory to the destination file
The nature of this question very much makes it look like a homework assignment.

Understand that EE's 'terms of use' do not permit experts to provide solutions to academic questions (i.e. experts are not allowed to do other's homework).

I feel like I've already pushed the limits of what EE allows by providing you an outline of how to complete this assignment.  

As such, I'm going to have to limit further responses to answering specific questions... questions best asked by providing coding samples of what you've got so far where experts here can help with questions like 'why won't line 25 compile', or 'why does the program raise an error at line 10', or 'what function can I call to determine a file size'.
ASKER CERTIFIED SOLUTION
Avatar of vpcnc
vpcnc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vpcnc

ASKER

already stated in my post..