The MASM Forum

General => The Campus => Topic started by: gelatine1 on June 18, 2014, 05:19:02 AM

Title: Appending files
Post by: gelatine1 on June 18, 2014, 05:19:02 AM
I would like to append my file instead of overwriting it again but I couldn't find how to do it. I open my file like this

invoke CreateFile, addr file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL


And I read that the GENERIC_WRITE acces right also contains the FILE_APPEND_DATA access to a file but I don't actually see how I should actually append it since there is no AppendFile function or anything similar. I have also read this about the WriteFile function:

Quote
To write to the end of file, specify both the Offset and OffsetHigh members of the OVERLAPPED structure as 0xFFFFFFFF. This is functionally equivalent to previously calling the CreateFile function to open hFile using FILE_APPEND_DATA access.

So I assume my only possibility is creating this OVERLAPPED structure? Or do I have any other possibilities that are nicer ? (I don't really like to use a struct like that :p)

Thanks in advance,
Jannes
Title: Re: Appending files
Post by: dedndave on June 18, 2014, 05:31:04 AM
i would use OPEN_EXISTING, rather than OPEN_ALWAYS

but, the function you want is either SetFilePointer or SetFilePointerEx

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365542%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365542%28v=vs.85%29.aspx)

with either function, you can use a pointer of 0, and use the FILE_END flag to point to the end of file
Title: Re: Appending files
Post by: gelatine1 on June 18, 2014, 05:53:10 AM
I assumed I just had to use

invoke SetFilePointer(hFile,NULL,NULL,FILE_END);

After the createfile and before the writefile function ? But it crashed when I did that. Or did I use it wrong ?

And is there any specific reason to use OPEN_EXISTING, rather than OPEN_ALWAYS ? I think it is handy to be able to create new files too and since I just use them for personal use I don't think it's an issue or is it ?
Title: Re: Appending files
Post by: hutch-- on June 18, 2014, 10:04:35 AM
Normally you check to see if you have a valid file handle after either opening an existing file OR creating another file. To append to an existing file, once you have a valid file handle, as Dave said, set the file pointer to the end of the existing file while the file is open then write the extra data you want to the end of the file.
Title: Re: Appending files
Post by: dedndave on June 18, 2014, 10:24:27 AM
OPEN_ALWAYS is fine, as long as you have read the description for that flag and it suits your needs

this bothers me a bit, though
invoke SetFilePointer(hFile,NULL,NULL,FILE_END);

i can see you are a C programmer   :biggrin:

correct masm syntax is
invoke SetFilePointer,hFile,0,0,FILE_END

no parens for INVOKE
the trailing semicolon means something different in masm
all after a semicolon is a comment

NULL is ok, but it's a constant "that just happens to be 0"
what you really want is 0 in this case
they are lo and hi dwords representing distance
Title: Re: Appending files
Post by: gelatine1 on June 19, 2014, 04:29:11 AM
Whoops sorry for that enormous syntax error  :redface:
I was in a hurry and I just copied the MSDN reference and altered the values I needed forgot about the rest... I am a C++ programmer by the way I only use the C references for all those kernel functions

Thanks for the help by the way!