The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Celtic on January 27, 2010, 05:58:22 PM

Title: delete bytes from a textfile
Post by: Celtic on January 27, 2010, 05:58:22 PM
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?

::)
Title: Re: delete bytes from a textfile
Post by: donkey on January 27, 2010, 06:50:10 PM
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.
Title: Re: delete bytes from a textfile
Post by: 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.
Title: Re: delete bytes from a textfile
Post by: jj2007 on January 28, 2010, 07:49:44 AM
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 (http://www.masm32.com/board/index.php?topic=12460)

.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
Title: Re: delete bytes from a textfile
Post by: Celtic on January 28, 2010, 07:58:03 AM
Thanks for the quick reply  :U
Title: Re: delete bytes from a textfile
Post by: 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.
Title: Re: delete bytes from a textfile
Post by: jj2007 on January 28, 2010, 08:55:53 AM
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 (http://www.masm32.com/board/index.php?topic=12460)

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
Title: Re: delete bytes from a textfile
Post by: 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
Title: Re: delete bytes from a textfile
Post by: jj2007 on January 28, 2010, 11:24:46 AM
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 (http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx) is needed in the "for output" versions. It does no harm, anyway.
Title: Re: delete bytes from a textfile
Post by: sinsi on January 28, 2010, 11:46:41 AM
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).
Title: Re: delete bytes from a textfile
Post by: dedndave on January 28, 2010, 12:14:15 PM
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
Title: Re: delete bytes from a textfile
Post by: 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...
Title: Re: delete bytes from a textfile
Post by: jj2007 on January 28, 2010, 02:05:39 PM
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