Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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;
}
 
Posted 28-Mar-13 21:42pm
Comments
CPallini at 29-Mar-13 4:55am
   
There is nothing wrong. However, you just create an empty file (you did not write to).
Jonathan [Darka] at 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
good
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.
  Permalink  
Comments
CPallini at 29-Mar-13 4:56am
   
I doubt the operating system is entitled to throw away it.
Matthew Faithfull at 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 at 29-Mar-13 5:24am
   
As far as I know handles are closed by Windows, on process exit (maybe I am wrong).
Matthew Faithfull at 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 at 29-Mar-13 5:52am
   
It looks that Windows even flushes the buffer content (if you've used WriteFile), see
http://stackoverflow.com/questions/7281255/why-is-data-not-being-flushed-to-file-on-process-exit
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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
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