不显示由CreateFile()函数创建的文本文件 [英] Text File created by CreateFile() function is not displayed

查看:103
本文介绍了不显示由CreateFile()函数创建的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个文本文件。如果文件不存在,则必须首先创建并打开它。

我为此目的编写了以下代码。代码工作正常,它还在BIN文件夹中创建文件但是当我执行此代码时仍然看不到任何文件被打开。

请告诉我的代码有什么问题。



CODE SNIPPET:

I am trying to open a text file. if the file does not exists, then it must be first created and opened.
I have written the following piece of code for this purpose. The code works fine, it also creates file inside BIN folder but still I cannot see any file get opened when I exexute this code.
Please tell what is wrong with my code.

CODE SNIPPET:

#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''m not sure exactly what you mean but I wouldn''t expect the above code to show you any file. You open a file but if it works you don''t write anything to it and you don''t close it so when your program ends the operating system is entitled to just throw the file away without ever writing to the disk.
Write something to the file and then close it and you may get what you want when the OS decides to write to disk from the cache.


这篇关于不显示由CreateFile()函数创建的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆