Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to create Ini file and enter/retrieve data from it VC++ MFC [modified]

How to create Ini file and enter/retrieve data from it VC++ MFC [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++questiontutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anna mathew
    wrote on last edited by
    #1

    This is the code i used ..........To CREATE a File and enter a default string.. ...........Ive TO USE VC++ MFC void CFileApp::OnInsert() { HANDLE hFile = CreateFile(_T("C:\\Documents and Settings\\MyFile.ini"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) AfxMessageBox(_T("Couldn't create the file!")); else { // Attach a CFile object to the handle we have. CFile myFile(hFile); static const char sz[] = "Hockey is best!"; // write string, without null-terminator myFile.Write(sz, lstrlen(sz)); myFile.Close(); } } This works..... BUt For INI I want to ENTER DATA SECTIN-WISE.... ........... Here Data is written just like in Text files... I need to use ...GetProfileString.... WriteProfileString..... n so on... how do i do it?? I NEED TO MAKE A PHONE DIRECTORY USING INI FILE......

    C CPalliniC 2 Replies Last reply
    0
    • A anna mathew

      This is the code i used ..........To CREATE a File and enter a default string.. ...........Ive TO USE VC++ MFC void CFileApp::OnInsert() { HANDLE hFile = CreateFile(_T("C:\\Documents and Settings\\MyFile.ini"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) AfxMessageBox(_T("Couldn't create the file!")); else { // Attach a CFile object to the handle we have. CFile myFile(hFile); static const char sz[] = "Hockey is best!"; // write string, without null-terminator myFile.Write(sz, lstrlen(sz)); myFile.Close(); } } This works..... BUt For INI I want to ENTER DATA SECTIN-WISE.... ........... Here Data is written just like in Text files... I need to use ...GetProfileString.... WriteProfileString..... n so on... how do i do it?? I NEED TO MAKE A PHONE DIRECTORY USING INI FILE......

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Do you plan to use as configuration file for your application ? If yes, this is a bit an outdated way of doing this. More recent applications use the registry to store config options. If you choose to use the registry, search for articles on CP, there are really a lot. They should get you started.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        Do you plan to use as configuration file for your application ? If yes, this is a bit an outdated way of doing this. More recent applications use the registry to store config options. If you choose to use the registry, search for articles on CP, there are really a lot. They should get you started.

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        A Offline
        A Offline
        anna mathew
        wrote on last edited by
        #3

        No... Im not using INI File to store confiq info... IM USING INI FILE TO CREATE A PHONE DIRECTORY... i know there r better methods... BUT ITS MY ASSIGNMENT.... ..........N ITS SAID THAT I USE ONLY INI FILE

        CPalliniC 1 Reply Last reply
        0
        • A anna mathew

          This is the code i used ..........To CREATE a File and enter a default string.. ...........Ive TO USE VC++ MFC void CFileApp::OnInsert() { HANDLE hFile = CreateFile(_T("C:\\Documents and Settings\\MyFile.ini"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) AfxMessageBox(_T("Couldn't create the file!")); else { // Attach a CFile object to the handle we have. CFile myFile(hFile); static const char sz[] = "Hockey is best!"; // write string, without null-terminator myFile.Write(sz, lstrlen(sz)); myFile.Close(); } } This works..... BUt For INI I want to ENTER DATA SECTIN-WISE.... ........... Here Data is written just like in Text files... I need to use ...GetProfileString.... WriteProfileString..... n so on... how do i do it?? I NEED TO MAKE A PHONE DIRECTORY USING INI FILE......

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          The following code snippet create two keys under the section 'General' of the File 'MyFile.ini', and then reads one of the keys, showing the value in a message box. (of course error check is left to the reader...) Please note 1: if the file doesn't exist, it creates both the file and the section. Please note 2: I used WritePrivateProfileString and GetPrivateProfileString, otherwise the I/O would happen inside the registry.

          const CString szINIFILE = _T("C:\\Documents and Settings\\anu\\beginning\\MyFile.ini");
          CString szFirstName;

          WritePrivateProfileString(_T("General"),_T("FirstName"), _T("Anu"), szINIFILE);
          WritePrivateProfileString(_T("General"),_T("LastName"), _T("???"), szINIFILE);

          GetPrivateProfileString(_T("General"), _T("FirstName"), _T(""), szFirstName.GetBuffer(MAX_PATH), MAX_PATH, szINIFILE);
          szFirstName.ReleaseBuffer();

          AfxMessageBox(szFirstName);

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          A 1 Reply Last reply
          0
          • A anna mathew

            No... Im not using INI File to store confiq info... IM USING INI FILE TO CREATE A PHONE DIRECTORY... i know there r better methods... BUT ITS MY ASSIGNMENT.... ..........N ITS SAID THAT I USE ONLY INI FILE

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            anna mathew wrote:

            IM USING INI FILE TO CREATE A PHONE DIRECTORY...

            As I already suggested, MFC object serialization, is far better for the purpose.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • CPalliniC CPallini

              The following code snippet create two keys under the section 'General' of the File 'MyFile.ini', and then reads one of the keys, showing the value in a message box. (of course error check is left to the reader...) Please note 1: if the file doesn't exist, it creates both the file and the section. Please note 2: I used WritePrivateProfileString and GetPrivateProfileString, otherwise the I/O would happen inside the registry.

              const CString szINIFILE = _T("C:\\Documents and Settings\\anu\\beginning\\MyFile.ini");
              CString szFirstName;

              WritePrivateProfileString(_T("General"),_T("FirstName"), _T("Anu"), szINIFILE);
              WritePrivateProfileString(_T("General"),_T("LastName"), _T("???"), szINIFILE);

              GetPrivateProfileString(_T("General"), _T("FirstName"), _T(""), szFirstName.GetBuffer(MAX_PATH), MAX_PATH, szINIFILE);
              szFirstName.ReleaseBuffer();

              AfxMessageBox(szFirstName);

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              A Offline
              A Offline
              anna mathew
              wrote on last edited by
              #6

              Thanks.... this works.... but i put the same lines of code in my MFC application.... still it works.... can u explain how? WriteProfileString...... is a console function... then how come it work with MFC?

              CPalliniC 1 Reply Last reply
              0
              • A anna mathew

                Thanks.... this works.... but i put the same lines of code in my MFC application.... still it works.... can u explain how? WriteProfileString...... is a console function... then how come it work with MFC?

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                WritePrivateProfileString is just a WIN32 API's function: Console applications, as well as GUI (and MFC) ones can profitably call it. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                A 1 Reply Last reply
                0
                • CPalliniC CPallini

                  WritePrivateProfileString is just a WIN32 API's function: Console applications, as well as GUI (and MFC) ones can profitably call it. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  A Offline
                  A Offline
                  anna mathew
                  wrote on last edited by
                  #8

                  Thank you

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups