visual c++ create text file
How to create text file?
CreateFile("1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_FLAG_OVERLAPPED,
NULL);
throw
1>------ Build started: Project: test2, Configuration: Debug Win32 ------ 1> test2.cpp 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(126): error C2065: 'GENERIC_READ' : undeclared identifier 1>c:\users\kredkołamacz\documents\vi开发者_如何学Pythonsual studio 2010\projects\test2\test2\Form1.h(126): error C2065: 'GENERIC_WRITE' : undeclared identifier 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(128): error C2065: 'NULL' : undeclared identifier 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(129): error C2065: 'CREATE_NEW' : undeclared identifier 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(130): error C2065: 'FILE_FLAG_OVERLAPPED' : undeclared identifier 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(131): error C2065: 'NULL' : undeclared identifier 1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(125): error C3861: 'CreateFile': identifier not found ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Include the Windows header file as follows at the top of your .h
or .cpp
files:
#include <windows.h>
This should solve the problems related to undefined symbols such as GENERIC_WRITE
and CreateFile
. As another poster mentioned, you should typically write your code in .cpp
files and only declare constants or classes in header files while placing the method implementations in .cpp
files along with regular functions.
The issues related to CreateFileW
once you get beyond this point need some more explanation:
By default, Windows applications generated from Visual Studio templates link against Unicode (wide character) versions of Windows APIs and have the UNICODE
C/C++ preprocessor macro defined to indicate this. When UNICODE
is defined, the preprocessor defines the symbol CreateFile
to expand to the name of the actual underlying Windows function name which is CreateFileW
where the W
suffix indicates it is a "wide-character", i.e. Unicode, function. If the UNICODE
macro is not defined (which can be overridden through various Visual Studio project settings), then CreateFile
will expand to the CreateFileA
symbol which is the name of the ANSI string version (A
for ANSI) of the function. 99% of the time you should use the default settings for UNICODE
as all modern versions of windows use Unicode characters internally.
Since CreateFileW
takes Unicode string arguments you need to pass L"1"
(i.e. a wide-character string literal) or use the TEXT
macro (e.g. TEXT("1")
) which will generate the correct string type corresponding to whether the UNICODE
compiler switch is defined or not.
Here's a link to the MSDN article about TEXT
: link.
精彩评论