c++ - 不显示由 CreateFile() 函数创建的文本文件

标签 c++ windows winapi text-files createfile

我正在尝试打开一个文本文件。如果该文件不存在,则必须首先创建并打开该文件。 我为此目的编写了以下代码。该代码工作正常,它还在 BIN 文件夹内创建文件,但当我执行此代码时,我仍然看不到任何文件被打开。 请告诉我的代码有什么问题。

代码片段:

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

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
  HANDLE  hFile;
  DWORD dwBytesRead, dwBytesWritten, dwPos;
  TCHAR szMsg[1000];

    hFile = CreateFile (("File.txt"),      // Open File.txt.
                        GENERIC_WRITE,          // Open for writing
                        0,                      // Do not share
                        NULL,                   // No security
                        OPEN_ALWAYS,            // Open or create
                        FILE_ATTRIBUTE_NORMAL,  // Normal file
                        NULL);                  // No template file

  if (hFile == INVALID_HANDLE_VALUE)
  {
    wsprintf (szMsg, TEXT("Could not open File.txt"));
    CloseHandle (hFile);            // Close the file.
    return 0;
  }

return 0;
}

最佳答案

I thought the parameter "OPEN_ALWAYS" of CREATE_FILE() will open the text file in front of me

不,它实际上不会打开您面前的文件,就像您在资源管理器中双击该文件一样。

相反,OPEN_ALWAYS 参数的含义是打开文件的句柄,以便您可以以编程方式读取或写入该文件 。如果您指定OPEN_ALWAYSCreateFile函数将成功创建文件并打开它的句柄,即使文件已经存在。

如果您不想要这种行为,您可以指定OPEN_EXISTING,这样仅当文件(或设备)已存在时才打开该文件(或设备)的句柄。如果不存在,CreateFile函数将返回错误。

请记住,正如其他人指出的那样,您需要在每次成功调用 CreateFile 后调用 CloseHandle。这可以确保您打开的文件(或设备)的句柄被正确释放,并防止您的应用程序泄漏资源。但仅当对 CreateFile 的调用成功时才需要执行此操作。如果失败并返回 INVALID_HANDLE_VALUE,则不应为该句柄调用 CloseHandle

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    HANDLE hFile;
    DWORD  dwBytesRead, dwBytesWritten, dwPos;
    TCHAR  szMsg[1000];

    // If the file already exists, open a handle to it for writing.
    // If the file does not exist, create it and then open a handle to it.
    hFile = CreateFile(TEXT("File.txt"),       // Open File.txt.
                       GENERIC_WRITE,          // Open for writing
                       0,                      // Do not share
                       NULL,                   // No security
                       OPEN_ALWAYS,            // Open or create
                       FILE_ATTRIBUTE_NORMAL,  // Normal file
                       NULL);                  // No template file

    // Test for and handle failure...
    if (hFile == INVALID_HANDLE_VALUE)
    {
        wsprintf(szMsg, TEXT("Could not open File.txt"));
        MessageBox(NULL, szMsg, NULL, MB_OK | MB_ICONERROR);
        // don't close the file here because it wasn't opened!
        return 0;
    }

    // Read from, write to, or otherwise modify the file here,
    // using the hFile handle.
    // 
    // For example, you might call the WriteFile function.
    // ...

    // Once we're finished, close the handle to the file and exit.
    CloseHandle (hFile);            // Close the file.
    return 0;
}

MSDN 上有完整的示例:Opening a File for Reading or Writing

如果您想像在资源管理器中双击一样打开文本文件,则需要使用ShellExecute function 。它不需要文件的句柄,只需要路径。当然,open 动词就是您要指定的动词。请注意,当您尝试使用 ShellExecute 打开文件时,您不应该拥有该文件的打开句柄。如果您使用 CreateFile 打开/创建了文件,请确保在调用 ShellExecute 之前调用 CloseHandle/p>

关于c++ - 不显示由 CreateFile() 函数创建的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15698690/

相关文章:

c - 在 C 中取证删除硬盘上的文件

c++ - DeleteFile 在最近关闭的文件上失败

c++ - 进入临界区死锁

c++ - 使用四元数 boost QVM 旋转

java - 为什么我不能通过套接字连接android和PC?

c++ - MinGW:C++ 编译器无法创建可执行文件

c# - 无法连接到 StreamSocketListener

winapi - ASLR 会导致地址与 DLL 注入(inject)的冲突吗?

c++ - 在哪里/如何删除另一个对象中的对象,在创建它的函数之外

Windows 'start/b' 命令问题