News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

delete bytes from a textfile

Started by Celtic, January 27, 2010, 05:58:22 PM

Previous topic - Next topic

Celtic

Hi i need help.
I will delete bytes from a textfile.
for example i have the word "hello" in the textfile. How can I delete this?

With setfilepointer api on the bytes? and then delete?

::)

donkey

You would normally open the file using CreateFile, after that you would read it into memory using ReadFile. Edit the file in memory then save the result using WriteFile. You will most likely want to move the existing file to the recycle bin before the write operation so you can use the same name (use DeleteFile for this). The other option is to memory map the file and edit it.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

raymond

After editing the file in memory and writing the modified file using WriteFile, remember to also use SetEndOfFile. Otherwise, the size of your file will not change and the last few bytes will still exist in the modified file.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

jj2007

Quote from: raymond on January 28, 2010, 03:17:32 AM
After editing the file in memory and writing the modified file using WriteFile, remember to also use SetEndOfFile. Otherwise, the size of your file will not change and the last few bytes will still exist in the modified file.

Normally, that should not be necessary, unless you used OPEN_ALWAYS:
Quote  invoke CreateFile, OpenF$, GENERIC_WRITE, FILE_SHARE_READ,
  NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0   ; WRONG!!

  invoke CreateFile, OpenF$, GENERIC_WRITE, FILE_SHARE_READ,
  NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0   ; RIGHT!!

Tested with the example below (written in Pure Masm(TM)), and the result is "Short string" ;-)

Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; get it here

.code
start:
   Open "O", #1, "TestShort.txt"
   Print #1, "This is a pretty long string"
   Close
   Open "O", #1, "TestShort.txt"
   Print #1, "Short string"
   Close
   
Exit
end start

Celtic


sinsi

If you comment out the line 'Print #1, "Short string"' you end up with a 0-byte file, though.
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on January 28, 2010, 08:14:32 AM
If you comment out the line 'Print #1, "Short string"' you end up with a 0-byte file, though.

Yes, that's by design - Basic behaves like that. I don't know C but I assume it's similar. The "U" and "A" variants are shown below.

Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; get it here

Seek MACRO file, offset
  ifdif @SubStr(<file>, 1, 1), <#>
   echo *** expected: Seek #n, offset ***
   .err
  endif
  invoke SetFilePointer, [MbFH+4*@SubStr(<file>, 2, 1)], offset, 0, FILE_BEGIN
ENDM

.code
start:
   Open "O", #1, "TestShort.txt"   ; open file for output
   Print #1, "This is a pretty long string"
   Close
   Open "U", #1, "TestShort.txt"   ; open file for updating
   Seek #1, 17         ; move file pointer
   Print #1, "short string"
   Close
   Open "A", #1, "TestShort.txt"   ; open file for appending
   Print #1, CrLf$, "... and this is an extra line"
   Close
   Exit
end start

Final result:
This is a pretty short string
... and this is an extra line

sinsi

Curious, what flags are used with O, U and A? The readme was a bit skimpy  :P
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on January 28, 2010, 09:09:57 AM
Curious, what flags are used with O, U and A? The readme was a bit skimpy  :P

"O":
  mov ecx, CREATE_ALWAYS
"A" and "U":
  mov ecx, OPEN_ALWAYS
  invoke CreateFile, OpenF$, GENERIC_WRITE, FILE_SHARE_READ,
  NULL, ecx, FILE_ATTRIBUTE_NORMAL, 0

Open "I":
invoke CreateFile, OpenF$, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0


I am a bit uncertain whether FILE_SHARE_READ is needed in the "for output" versions. It does no harm, anyway.

sinsi

So CREATE_ALWAYS will truncate a file if it exists?

Quote from: jj2007I am a bit uncertain whether FILE_SHARE_READ is needed in the "for output" versions. It does no harm, anyway.
If I am writing to a file, I don't want another process accessing it until I have finished and closed the handle, so I always use 0.
FILE_SHARE_READ means that another process can open the file to read while you are writing to it...

In the same way, I open a file for reading without FILE_SHARE_WRITE, because dammit it can wait until I've read it first (open-read-close).
Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

Quote...because dammit it can wait until I've read it first (open-read-close).
lol Sinsi - you and i think alike
however, i usually think "what would some other process want with my dinky insiginifigant file in the first place ?" - lol
another process would have to know it existed, first   :P
so - trying to think ahead - that other process is most likely to be one written by me

sinsi

Dave, could be an av/malware scanner, could be an indexing service (not just windows, nero has one too).
heh, could be malware scanning files for email addresses...
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on January 28, 2010, 12:33:09 PM
Dave, could be an av/malware scanner, could be an indexing service (not just windows, nero has one too).
heh, could be malware scanning files for email addresses...

While that scenario is perfectly realistic, the only method to prevent the Axis of Evil from reading your file requires two steps:

1. You must use CreateFile with Flag NULL
2. As soon as you have finished writing, delete the file before the malware can access the contents.


:wink