views:

26

answers:

1

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\visual 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 ==========

+1  A: 

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.

Richard Cook
Not working, I added it to .cpp file
asd
Based on your error output you have the code in a header file. You need to put the windows.h BEFORE the use of those symbols. Generally you don't put code in header files. (all the template nonsense notwithstanding)
Tim
When I add it to .h file:'CreateFileW' : cannot convert parameter 1 from 'const char [2]' to 'LPCWSTR'1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
asd
You are going to have to do at least a LITTLE work here. I am sure there is somewhere online that you can google that error message... good luck
Tim
You need to use the `TEXT` macro to define your string, e.g. `TEXT("1")` or use the `L` prefix to specify a Unicode string literal as follows: `L"1"`.
Richard Cook