Click here to Skip to main content
13,093,184 members (80,590 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.

#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
    wsprintf (szMsg, TEXT("Could not open File.txt"));
    CloseHandle (hFile);            // Close the file.
    return 0;
return 0;
Posted 28-Mar-13 20:42pm
CPallini 29-Mar-13 4:55am
There is nothing wrong. However, you just create an empty file (you did not write to).
Jonathan [Darka] 29-Mar-13 5:47am
What do you mean you can't see any file get opened?

You say the file get created, so clearly the code is working - what is it you are expecting that doesn't happen?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
CPallini 29-Mar-13 4:56am
I doubt the operating system is entitled to throw away it.
Matthew Faithfull 29-Mar-13 5:10am
Maybe not on UNIX but I wouldn't be surprised on Windows if the file never shows in Explorer. It has no extent, no content, no format, can't be read from or accessed by any other process and the handle having been effectively leaked belongs to the Kernel but isn't safe to do anything with, perhaps even to close given potential security issues with file system caches and handle stealing. Maybe it will show up maybe not but I wouldn' count on it.
CPallini 29-Mar-13 5:24am
As far as I know handles are closed by Windows, on process exit (maybe I am wrong).
Matthew Faithfull 29-Mar-13 5:32am
They are but there's more than one way to skin a cat as we say. ( I wonder frankly why more than one way would be needed given the rare occassions when skinning a cat is necessary )
I think in the case of leaked handles Windows effectively just deletes them along with any Kernel resources rather than going through the full process that a file system 'close' might entail. An MS insider would be needed to confirm but I think in this case you'd just end up with a dead entry in the disk cache effectively some temporarily leaked memory.
CPallini 29-Mar-13 5:52am
It looks that Windows even flushes the buffer content (if you've used WriteFile), see
However I definitely think it is a bad practice leaving open handles.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100