0

Here's my code in which I've got on an infinite loop (to my knowledge)

while(true) {
    DWORD TitleID = XamGetCurrentTitleId();
    std::ostringstream titleMessageSS;
    titleMessageSS << "Here's the current title we're on : " << TitleID << "\n\n";
    std::string titleMessage = titleMessageSS.str(); // get the string from the stream
    DWORD dwBytesToWrite = (DWORD)titleMessage.size();
    DWORD dwBytesWritten = 0;
    BOOL bErrorFlag = FALSE;
    HANDLE logFile = CreateFile( "Hdd:\\LOGFile.txt", GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
    bErrorFlag = WriteFile(logFile, titleMessage.c_str(), dwBytesToWrite, &dwBytesWritten, NULL);
    CloseHandle(logFile);
    Sleep(30000);
}
return NULL;

Does anyone see a reason as to why this only writes just once? I've waited over 5 minutes to see if it does anything in the end to no avail.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
Curtis
  • 2,646
  • 6
  • 29
  • 53
  • That's all not standard c++, I'd seriously recommend you to use the classes and functions from the [Standard I/O library](http://en.cppreference.com/w/cpp/io),. Also note your `while(true)` loop never ends, it's probably not what you want to do. – πάντα ῥεῖ Feb 21 '15 at 10:44
  • Check for error returns on EVERY system call, (especially 'CreateFile', in this case). – Martin James Feb 21 '15 at 10:47
  • 2
    MSDN: CREATE_NEW: Creates a new file, only if it does not already exist. – Martin James Feb 21 '15 at 10:49
  • 1
    @πάνταῥεῖ: How do you learn about an operating system if you never look at the native API? – Emilio Garavaglia Feb 21 '15 at 10:56
  • 1
    @πάνταῥεῖ: The standard I/O library feels insanely crippled when compared to the native Windows I/O system. Overlapped I/O, File Mapping, or *delete on close* creation disposition are just a few aspects that are not at all reflected in the C++ Standard I/O library. – IInspectable Feb 21 '15 at 13:02
  • 1
    It's hopeless trying to work this out if you 1. Don't check for errors and 2. Don't perform basic debugging. – David Heffernan Feb 21 '15 at 13:14
  • @IInspectable _"... feels insanely crippled when compared to the native Windows I/O ..."_ That's just a ridiculous argument IMHO. Using the Standard I/O keeps your code portable for use on any OS at least. For edge cases I always can provide my own `std::streambuf` implementation, and it will still just work smoothly with all the rest of the c++ standard stuff. – πάντα ῥεῖ Feb 21 '15 at 17:14
  • @πάνταῥεῖ: You cannot implement a `std::streambuf` that miraculously exposes asynchronous file operations at the stream interface. Likewise, file mappings will not be exposed at the stream interface. [I/O Concepts](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365199.aspx) is required reading for anyone suggesting that Standard C++ file streams are Good Enough™ for everything. – IInspectable Feb 23 '15 at 19:07

2 Answers2

4

The Flag CREATE_NEW in CreateFile prevents the update of the file because CreateFile fail with ERROR_FILE_EXISTS. Use OPEN_ALWAYS instead. Also it will always truncate. Replace GENERIC_WRITE with FILE_APPEND_DATA if you want to add a new line at the end of your logfile.

The whole CreateFile line should be:

HANDLE logFile = CreateFile( "Hdd:\\LOGFile.txt", FILE_APPEND_DATA , 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

Read CreateFile documentation carefully, it worth it, because it has a central role in the windows IO universe: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

look also add: https://stackoverflow.com/a/9891875/1922748

Community
  • 1
  • 1
Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
0

As Martin James mentioned, from MSDN: CREATE_NEW

Creates a new file, only if it does not already exist.

If the specified file exists, the function fails and the last-error code is set to ERROR_FILE_EXISTS (80).

If the specified file does not exist and is a valid path to a writable location, a new file is created.

So it seems that the handle is invalid after the first call, and hence WriteFile() fails.

Maged Elghareib
  • 119
  • 2
  • 9