Thread: Using standard C fscanf function with win32 save dialog

  1. #1
    Registered User
    Join Date
    Nov 2001

    Using standard C fscanf function with win32 save dialog

    okay, i have a c function that reads from a file and populates a data structure, at present the filename is defined during development. what i was hoping for was to use the standard load dialog that comes with win32 to load different files

    the question can i use the c code directly or will i need to rewrite the function to talk with the HANDLE in win32

    my c code

    static void readFile(char path[MAXSTR]){
       FILE * log;
       char t;   // Temporary char variable for delimiters
       int id, conId;   // Temporary variables for the ID of an item and ID of a connection
       float trait[5], str;   // Trait[] holds the traits/levels of the item. str contains the strength
       log = fopen(path, "r");   // Open the file
       if(log != NULL){   // Check the file is opened correctly
    		while(!feof(log)){   // While the end of file has not been reached
    		    // Line Below: Reads in the item ID and 5 traits/levels.
    			fscanf(log,"%d, %f, %f, %f, %f, %f", &id, &trait[0], &trait[1], &trait[2], &trait[3], &trait[4]);   
    			fscanf(log,"%c", &t);   // Check if there is any connections
    			addNode(id, trait);   // Add the item to the data structure
    			if(t == '{'){   // There are connections to be read in
    				while(t == '{'){   // While there are connections available
    					fscanf(log,"%d, %f", &conId, &str );   // Read in conId and strength of connection
    					addConnection(id, conId, str);   // Add the connection to the list
    					fscanf(log,"%c %c", &t, &t);   // Check if there are more connections
    	   fclose(log);   // Close the file
    my win32 code (unnecessary code has been commented out)

    BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
    	HANDLE hFile1; //file handler
    	BOOL bSuccess = FALSE; //success flag, default set to FALSE
    	hFile1 = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    		OPEN_EXISTING, 0, NULL); //open file dialog
    	if(hFile1 != INVALID_HANDLE_VALUE) //error if cant get dialog handle
    		DWORD dwFileSize; //Variable to store file size
    		dwFileSize = GetFileSize(hFile1, NULL); //assign value to variable
    		if(dwFileSize != 0xFFFFFFFF)
    			//readFile(pszFileName, hFile1);
    			LPSTR pszFileText; //A 32-bit pointer to the contents of the edit window
    			pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);//allocate the specified bytes from the heap
    			if(pszFileText != NULL) //no text, dont bother
    				DWORD dwRead;
    				if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
    					pszFileText[dwFileSize] = 0; // Add null terminator
    					if(SetWindowText(hEdit, pszFileText))
    						bSuccess = TRUE; // It worked!
    		CloseHandle(hFile1);//close the file
    	return bSuccess; //return the outcome, true or false
    the first code example reads from a file into a data structure the second reads from a file and populates an edit dialog window

    will this require much alterations and if so has anyone got any tips that will not require me to rewrite this or at least not much

    thanx in advance
    Last edited by korbitz; 04-08-2004 at 12:55 PM. Reason: me bad :(

  2. #2
    Registered User
    Join Date
    Nov 2001
    No matter, managed to fix it, thanx to all that looked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Calling up a dialog from a dialog handler function
    By Welder in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2007, 10:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
Website Security Test