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?
::)
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.
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.
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
Thanks for the quick reply :U
If you comment out the line 'Print #1, "Short string"' you end up with a 0-byte file, though.
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
Curious, what flags are used with O, U and A? The readme was a bit skimpy :P
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.
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).
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
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...
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