-1

i wrote some code to createfile in c++ it compiles without errors, but it doesn't create any file.. Can anyone tell me what is wrong ? thanks

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

    int main()
    {
        HANDLE hfile;
        char data[] = "some text to write into file";       

        hfile = CreateFile(
               L"c:\name.txt",              
               GENERIC_WRITE, 
               0,
               NULL,
               CREATE_NEW,
               FILE_ATTRIBUTE_NORMAL,
               NULL);

        /*
        if (hFile == INVALID_HANDLE_VALUE) 
        { 
            cout << "Unable to create file \n";

        }
        */
    return 0;
    }
4

2 回答 2

4

同样在 C++ 中,您需要转义反斜杠,即

"C:\\mydirectory\\"...
于 2012-09-22T19:11:36.723 回答
2

如果尝试将其创建到根目录中,如果没有以管理员身份登录,您可能会遇到权限问题。尝试在子目录中创建它。

于 2012-09-22T19:08:45.120 回答