Simple File Write

This is a discussion on Simple File Write within the Windows Programming forums, part of the Platform Specific Boards category; The Following code is used to keep a log about my process' activity. The problem is that it the debug ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    48

    Simple File Write

    The Following code is used to keep a log about my process' activity. The problem is that it the debug output file gets the wrong name (unreadable characters). I can't figure out where my pointers go wild. Sorry for such a silly problem .
    Code:
    #include "includes.h"
    #include "externs.h"
    
    
    HANDLE hDebug;
    HANDLE mDebug;
    
    
    void AddLog(char *buffer)
    {
    	#ifdef DEBUG
    	DWORD dwBytesWritten=0;
    	DWORD dwBytesToWrite=0;
    	dwBytesToWrite =(DWORD) strlen(buffer);
    	if( hDebug == INVALID_HANDLE_VALUE || hDebug == NULL )
    	return;
    	WaitForSingleObject(mDebug, INFINITE);
    	
    	while(dwBytesWritten < dwBytesToWrite)
    	{
    					
    		WriteFile(hDebug, buffer+dwBytesWritten, dwBytesToWrite - dwBytesWritten, &dwBytesWritten,NULL);
    		
    	}	
    	
    	ReleaseMutex(mDebug);
    	#endif
    }
    
    void StartDebug()
    {
    
    	wchar_t FileDebug[]={'d','e','b','u','g','.','e','t','h'};
    	mDebug = CreateMutex(NULL, false, NULL);
    	hDebug = CreateFile( (LPWSTR) FileDebug,
    						GENERIC_WRITE,
    						0,
    						NULL,
    						CREATE_ALWAYS,
    						FILE_ATTRIBUTE_NORMAL,
    						NULL
    						);
    			
    						
    	
    						
    								
    
    }

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    WriteFile must be using ANSI, try WriteFileW or define UNICODE in your header files, also you con use UNICODE as string type in your project's options.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Code:
    wchar_t FileDebug[]={'d','e','b','u','g','.','e','t','h'};
    That's not right. CreateFile takes a null-terminated string, but you've declared it in such a way as to deliberately not null-terminate it for some reason.

    You could simply write this:
    Code:
    wchar_t FileDebug[] = L"debug.eth";
    or better yet just:
    Code:
    const wchar_t *FileDebug = L"debug.eth";
    if you don't need to modify that string.

    You might also benefit from using TCHAR instead of wchar_t to be agnostic to the unicode setting of your project. So the best option is probably:
    Code:
    const TCHAR *FileDebug = _T("debug.eth");
    and you can drop the (LPWSTR) part.
    Last edited by iMalc; 11-13-2009 at 02:10 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    Makes sense now. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write To a File Without Deleting
    By SamGray in forum C Programming
    Replies: 2
    Last Post: 09-06-2009, 11:56 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 1
    Last Post: 04-02-2003, 06:50 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21