Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C++] Scrivere e leggere file .ini

    Ho cercato su internet un modo per salvare e leggere le impostazioni del mio programma con un file .ini: ho trovato questo esempio nel sito della microsoft, solo che c'è un problema: non mi ritrovo nessun file .ini una volta lanciato il programma! E se provo a crearlo manualmente, me lo ritrovo comunque vuoto. Sapete consigliarmi qualcosa di meglio?

    codice:
    #include <windows.h> 
    #include <tchar.h>
    #include <stdio.h> 
     
    int main() 
    { 
       TCHAR   inBuf[80]; 
       HKEY   hKey1, hKey2; 
       DWORD  dwDisposition; 
       LONG   lRetCode; 
       TCHAR   szData[] = TEXT("USR:App Name\\Section1");
     
       // Create the .ini file key. 
       lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE, 
           TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping\\appname.ini"), 
           0, 
           NULL, 
           REG_OPTION_NON_VOLATILE, 
           KEY_WRITE, 
           NULL, 
           &hKey1, 
           &dwDisposition); 
     
       if (lRetCode != ERROR_SUCCESS)
       { 
          printf ("Error in creating appname.ini key (%d).\n", lRetCode); 
          return (0) ; 
       } 
     
       // Set a section value 
       lRetCode = RegSetValueEx ( hKey1, 
                                  TEXT("Section1"), 
                                  0, 
                                  REG_SZ, 
                                  (BYTE *)szData, 
                                  sizeof(szData)); 
     
       if (lRetCode != ERROR_SUCCESS) 
       { 
          printf ("Error in setting Section1 value\n"); 
          // Close the key
          lRetCode = RegCloseKey( hKey1 );
          if( lRetCode != ERROR_SUCCESS )
          {
             printf("Error in RegCloseKey (%d).\n", lRetCode);
             return (0) ; 
          }
       } 
     
       // Create an App Name key 
       lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER, 
                                   TEXT("App Name"), 
                                   0, 
                                   NULL, 
                                   REG_OPTION_NON_VOLATILE,
                                   KEY_WRITE, 
                                   NULL, 
                                   &hKey2, 
                                   &dwDisposition); 
     
       if (lRetCode != ERROR_SUCCESS) 
       { 
          printf ("Error in creating App Name key (%d).\n", lRetCode); 
    
    
          // Close the key
          lRetCode = RegCloseKey( hKey2 );
          if( lRetCode != ERROR_SUCCESS )
          {
             printf("Error in RegCloseKey (%d).\n", lRetCode);
             return (0) ; 
          }
       } 
     
       // Force the system to read the mapping into shared memory 
       // so that future invocations of the application will see it 
       // without the user having to reboot the system 
       WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" ); 
     
       // Write some added values 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("FirstKey"), 
                                  TEXT("It all worked out OK."), 
                                  TEXT("appname.ini")); 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("SecondKey"), 
                                  TEXT("By golly, it works!"), 
                                  TEXT("appname.ini")); 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("ThirdKey"), 
                                  TEXT("Another test..."), 
                                  TEXT("appname.ini")); 
    
    
       // Test 
       GetPrivateProfileString (TEXT("Section1"), 
                                TEXT("FirstKey"), 
                                TEXT("Error: GPPS failed"), 
                                inBuf, 
                                80, 
                                TEXT("appname.ini")); 
       _tprintf (TEXT("Key: %s\n"), inBuf); 
     
       // Close the keys
       lRetCode = RegCloseKey( hKey1 );
       if( lRetCode != ERROR_SUCCESS )
       {
          printf("Error in RegCloseKey (%d).\n", lRetCode);
          return(0);
       }
    
    
       lRetCode = RegCloseKey( hKey2 );
       if( lRetCode != ERROR_SUCCESS )
       {
          printf("Error in RegCloseKey (%d).\n", lRetCode);
          return(0);
       }
       
       return(1); 
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,461
    Quel codice non scrive su file ma sull registro.

    Esistono mille esempi su Internet per i file .ini, anche come classi, vedi

    http://www.codeproject.com/Articles/5401/CIni

    ma potresti anche usare un più moderno file xml ...

    Se vuoi, dai un'occhiata alle funzioni come

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Ultima modifica di oregon; 15-01-2014 a 17:47
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Quote Originariamente inviata da oregon
    Questo codice è del 2003, pensi che sia ancora valido?

    Quote Originariamente inviata da oregon
    Se noti bene, il codice che ho postato l'ho preso proprio da qua

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.