开发者

how to create a file using windows API?

I want to create a text file using windows API. I studied about

HANDLE CreateFile(
LPCTSTR lpFileName, 
DWORD dwDesiredAccess, 
DWORD dwShareMode, 
LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
DWORD dwCreationDispostion , 
DWORD dwFlagsAndAttributes, 
HANDLE hTemplateFile); 

I can't understand how to pass the first parameter to create a file. I tried to use data-type FILE and pass it's pointer as first parameter but it's showing incompatible with LP开发者_如何学PythonCTSTR. Can anybody tell me how to do it? I am new in using windows API. Thanks in advance.


LPCTSTR is the same as const TCHAR* ("Long Pointer to a Constant TCHAR-String"), which is either const wchar_t* or const char* depending on the character set. So just pass in a string surrounded by _T(), like:

CreateFile(_T("C:\\File.txt"), FILE_READ_DATA, FILE_SHARE_READ,
    NULL, OPEN_ALWAYS, 0, NULL);

(By the way, FILE is not part of the Windows SDK; it's part of the standard C runtime library, and it's internally based on CreateFile, which creates a file based on its name.)


Pass the name of the file. A string. Use the SDK example code.


To create file with CreateFile() function :

CreateFile("OUTPUT_FILE", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

The first parameter is the name of the file which you can pass from any string variable..for example if the filename is in CString variable you can pass (LPCTSTR) variblename to convert it to LPCTSTR. all the other parameters are depending on your implementation whether you want to create the file always or append to existing file etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜