Click here to Skip to main content
16,010,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
With the code listed below, new data overwrites the existing data. I want to append the new data to that already in the file

hFile = CreateFile ((LPCTSTR) szSaveName, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_APPEND_DATA , NULL) ;
bSuccess=WriteFile(hFile,sData,num,&written,NULL);

Regards,Roland

Posted
Comments
[no name] 23-Jun-14 10:28am    
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778(v=vs.85).aspx
Sergey Alexandrovich Kryukov 23-Jun-14 11:56am    
Visual Studio is just the IDE. It does not open files of your applications and does not even build them... :-)
—SA
Sir Roland 23-Jun-14 12:28pm    
Yes, that's true. Perhaps you didn't notice. I'm referring to Visual Studio C++.
Sergey Alexandrovich Kryukov 23-Jun-14 13:15pm    
I noticed that...
—SA

Why not just use the (portable) STL? Just create an fstream with read and write permissions (which is the default), move the write-cursor to the end of the buffer and start writing. A demonstrating example:

C++
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    fstream file("somefile");
    file.seekp(0, ios_base::end);

    file << "\nadditional stuff";
}
 
Share this answer
 
v2
Comments
Sir Roland 25-Jun-14 22:04pm    
Thanks for the suggestion. I'll probably go with Solution 2.
Joren Heit 30-Jun-14 7:53am    
Sure, always go with the non-portable solution.
Take a look at MSDN: CreateFile function.

The documentation says that the FILE_APPEND_DATA should go into the second parameter (dwDesiredAccess) and that FILE_WRITE_DATA should not be set in this case.

Have you tried that? Otherwise, I would go with the KarstenK's solution.
 
Share this answer
 
Comments
Sir Roland 25-Jun-14 22:02pm    
Thanks for the solution. I read the documentation, apparently not closely enough
regards, Roland
you need the function SetFilePointer
Here is a sample code from MS.
 
Share this answer
 
Comments
CPallini 23-Jun-14 12:33pm    
5.
Joren Heit 24-Jun-14 17:33pm    
Non-portable! Why not use STL when you can? See my answer :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900