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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with fwrite

problem with fwrite

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebugging
20 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.
  • C Offline
    C Offline
    Cadimi
    wrote on last edited by
    #1

    I wrote a short code to write the status of my program to a file, but when debug in that code, I realize that fwrite function still increase its file pointer but when I read that file by fread function, I can't read that value. This is my code: // TODO: Add your control notification handler code here //write config file FILE *fout = fopen( "config.pcm", "wb" ); if ( fout ) { //write the time of power function fwrite( &m_uTime, sizeof( int ), 1, fout ); unsigned long temp; //write tasks //number of day task temp = m_alltasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); for ( int i = 0; i < m_alltasks.size(); i++ ) { //date fwrite( &m_alltasks[i].date, sizeof( MYDATE ), 1, fout ); //number of notes in that day temp = m_alltasks[i].tasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); //notes for ( int j = 0; j < m_alltasks[i].tasks.size(); j++ ) { fwrite( &m_alltasks[i].tasks[j], sizeof( TASK ), 1, fout ); } } //how many hotkeys have been defined temp = m_hkArr.size(); fwrite( &temp, sizeof( long ), 1, fout ); //write the list of hotkeys for ( int i = 0; i < temp; i++ ) fwrite( &m_hkArr[i], sizeof( HOTKEY ), 1, fout ); fclose( fout ); } else { MessageBox( _T( "Can't write config file" ), _T( "Warning" ) ); OnCancel(); } //unregister hotkeys for ( int i = 0; i < m_hkArr.size(); i++ ) { UnregisterHotKey( m_hWnd, m_hkArr[i].id ); GlobalDeleteAtom( m_hkArr[i].id ); } OnCancel(); They all work except for the code I bolded ( file pointer still increase, but the file is not change )... Help me plz >.<...

    CPalliniC D 2 Replies Last reply
    0
    • C Cadimi

      I wrote a short code to write the status of my program to a file, but when debug in that code, I realize that fwrite function still increase its file pointer but when I read that file by fread function, I can't read that value. This is my code: // TODO: Add your control notification handler code here //write config file FILE *fout = fopen( "config.pcm", "wb" ); if ( fout ) { //write the time of power function fwrite( &m_uTime, sizeof( int ), 1, fout ); unsigned long temp; //write tasks //number of day task temp = m_alltasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); for ( int i = 0; i < m_alltasks.size(); i++ ) { //date fwrite( &m_alltasks[i].date, sizeof( MYDATE ), 1, fout ); //number of notes in that day temp = m_alltasks[i].tasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); //notes for ( int j = 0; j < m_alltasks[i].tasks.size(); j++ ) { fwrite( &m_alltasks[i].tasks[j], sizeof( TASK ), 1, fout ); } } //how many hotkeys have been defined temp = m_hkArr.size(); fwrite( &temp, sizeof( long ), 1, fout ); //write the list of hotkeys for ( int i = 0; i < temp; i++ ) fwrite( &m_hkArr[i], sizeof( HOTKEY ), 1, fout ); fclose( fout ); } else { MessageBox( _T( "Can't write config file" ), _T( "Warning" ) ); OnCancel(); } //unregister hotkeys for ( int i = 0; i < m_hkArr.size(); i++ ) { UnregisterHotKey( m_hWnd, m_hkArr[i].id ); GlobalDeleteAtom( m_hkArr[i].id ); } OnCancel(); They all work except for the code I bolded ( file pointer still increase, but the file is not change )... Help me plz >.<...

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

      How did you check the file was not actually written? How did you define m_hkArr? How was HOTKEY defined? :)

      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

      In testa che avete, signor di Ceprano?

      C 1 Reply Last reply
      0
      • CPalliniC CPallini

        How did you check the file was not actually written? How did you define m_hkArr? How was HOTKEY defined? :)

        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

        C Offline
        C Offline
        Cadimi
        wrote on last edited by
        #3

        I do it on Visual Studio 2005 SP1. I added config.pcm into my solution. After I had run my program, if config.pcm changed the content, then Visual Studio would ask you to update the content of that file. When I debug, I see the file pointer increase, but after I closed fout, VS didn't ask me to update the content -> the content hadn't been modified. m_hkArr is vector m_hkArr. The structure of struct HOTKEY: struct HOTKEY { BOOL isCtrl; UINT c; TCHAR szPath[256]; int id; //id of this hot key }; This struct is self-identified Hotkey :D. I just check for Ctrl or Alt is press with the key. id is the ID of the hotkey, return value of GlobalAddAtom. The code I have posted just a bit of my code

        CPalliniC 1 Reply Last reply
        0
        • C Cadimi

          I do it on Visual Studio 2005 SP1. I added config.pcm into my solution. After I had run my program, if config.pcm changed the content, then Visual Studio would ask you to update the content of that file. When I debug, I see the file pointer increase, but after I closed fout, VS didn't ask me to update the content -> the content hadn't been modified. m_hkArr is vector m_hkArr. The structure of struct HOTKEY: struct HOTKEY { BOOL isCtrl; UINT c; TCHAR szPath[256]; int id; //id of this hot key }; This struct is self-identified Hotkey :D. I just check for Ctrl or Alt is press with the key. id is the ID of the hotkey, return value of GlobalAddAtom. The code I have posted just a bit of my code

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

          Are you sure you're writing to the same file that is open by Visual Studio?

          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

          In testa che avete, signor di Ceprano?

          C 1 Reply Last reply
          0
          • CPalliniC CPallini

            Are you sure you're writing to the same file that is open by Visual Studio?

            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

            C Offline
            C Offline
            Cadimi
            wrote on last edited by
            #5

            yes, When I change the config of my program, so, it have to write in the config.pcm. All the configurations of my program are written on config.pcm except for the hotkeys ( the bolded code )

            CPalliniC 1 Reply Last reply
            0
            • C Cadimi

              yes, When I change the config of my program, so, it have to write in the config.pcm. All the configurations of my program are written on config.pcm except for the hotkeys ( the bolded code )

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

              How can you see the file pointer advancing? How many HOTKEY items did you successfully write (i.e. Did you check temp value? Did you check fwrite return value?)? :)

              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

              In testa che avete, signor di Ceprano?

              C 1 Reply Last reply
              0
              • CPalliniC CPallini

                How can you see the file pointer advancing? How many HOTKEY items did you successfully write (i.e. Did you check temp value? Did you check fwrite return value?)? :)

                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

                C Offline
                C Offline
                Cadimi
                wrote on last edited by
                #7

                when debuging, just watch for the file pointer, so, I can see fout increase the value, I write 1 or 2 HOTKEY to try, but the content no change. When I read the file, the value is 0 >.<

                CPalliniC 1 Reply Last reply
                0
                • C Cadimi

                  when debuging, just watch for the file pointer, so, I can see fout increase the value, I write 1 or 2 HOTKEY to try, but the content no change. When I read the file, the value is 0 >.<

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

                  Cadimi wrote:

                  I write 1 or 2 HOTKEY to try

                  But then do you close the file?

                  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

                  In testa che avete, signor di Ceprano?

                  C 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Cadimi wrote:

                    I write 1 or 2 HOTKEY to try

                    But then do you close the file?

                    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

                    C Offline
                    C Offline
                    Cadimi
                    wrote on last edited by
                    #9

                    as you can see in the code, I closed it, and before closed it, I fflush( fout ) carefully :D

                    CPalliniC 1 Reply Last reply
                    0
                    • C Cadimi

                      as you can see in the code, I closed it, and before closed it, I fflush( fout ) carefully :D

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

                      Yes, I saw the code, anyway I was referring to you debugging actions (i.e. I didn't know if you stopped the program after few write operations). The reason behind my questions is I cannot believe fwrite silently failing, it never happened to me. Can't you isolate the failing piece of the writing routine and run it in a simpler context (for instance another, adhoc, project)?

                      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

                      In testa che avete, signor di Ceprano?

                      C 1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        Yes, I saw the code, anyway I was referring to you debugging actions (i.e. I didn't know if you stopped the program after few write operations). The reason behind my questions is I cannot believe fwrite silently failing, it never happened to me. Can't you isolate the failing piece of the writing routine and run it in a simpler context (for instance another, adhoc, project)?

                        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

                        C Offline
                        C Offline
                        Cadimi
                        wrote on last edited by
                        #11

                        thanks for your help, that's the worse thing I've ever met >.<

                        1 Reply Last reply
                        0
                        • C Cadimi

                          I wrote a short code to write the status of my program to a file, but when debug in that code, I realize that fwrite function still increase its file pointer but when I read that file by fread function, I can't read that value. This is my code: // TODO: Add your control notification handler code here //write config file FILE *fout = fopen( "config.pcm", "wb" ); if ( fout ) { //write the time of power function fwrite( &m_uTime, sizeof( int ), 1, fout ); unsigned long temp; //write tasks //number of day task temp = m_alltasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); for ( int i = 0; i < m_alltasks.size(); i++ ) { //date fwrite( &m_alltasks[i].date, sizeof( MYDATE ), 1, fout ); //number of notes in that day temp = m_alltasks[i].tasks.size(); fwrite( &temp, sizeof( long ), 1, fout ); //notes for ( int j = 0; j < m_alltasks[i].tasks.size(); j++ ) { fwrite( &m_alltasks[i].tasks[j], sizeof( TASK ), 1, fout ); } } //how many hotkeys have been defined temp = m_hkArr.size(); fwrite( &temp, sizeof( long ), 1, fout ); //write the list of hotkeys for ( int i = 0; i < temp; i++ ) fwrite( &m_hkArr[i], sizeof( HOTKEY ), 1, fout ); fclose( fout ); } else { MessageBox( _T( "Can't write config file" ), _T( "Warning" ) ); OnCancel(); } //unregister hotkeys for ( int i = 0; i < m_hkArr.size(); i++ ) { UnregisterHotKey( m_hWnd, m_hkArr[i].id ); GlobalDeleteAtom( m_hkArr[i].id ); } OnCancel(); They all work except for the code I bolded ( file pointer still increase, but the file is not change )... Help me plz >.<...

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Cadimi wrote:

                          FILE *fout = fopen( "config.pcm", "wb" );

                          Are you using a relative path? What is the value of temp?

                          "Love people and use things, not love things and use people." - Unknown

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                          C 1 Reply Last reply
                          0
                          • D David Crow

                            Cadimi wrote:

                            FILE *fout = fopen( "config.pcm", "wb" );

                            Are you using a relative path? What is the value of temp?

                            "Love people and use things, not love things and use people." - Unknown

                            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                            C Offline
                            C Offline
                            Cadimi
                            wrote on last edited by
                            #13

                            temp is the number of hotkeys, its value is the size of the vector contain my HOTKEY struct

                            D 1 Reply Last reply
                            0
                            • C Cadimi

                              temp is the number of hotkeys, its value is the size of the vector contain my HOTKEY struct

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              Cadimi wrote:

                              temp is the number of hotkeys, its value is the size of the vector contain my HOTKEY struct

                              I know what it represents. I asked what its value was (at the time of writing to the file).

                              "Love people and use things, not love things and use people." - Unknown

                              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                              C 1 Reply Last reply
                              0
                              • D David Crow

                                Cadimi wrote:

                                temp is the number of hotkeys, its value is the size of the vector contain my HOTKEY struct

                                I know what it represents. I asked what its value was (at the time of writing to the file).

                                "Love people and use things, not love things and use people." - Unknown

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                C Offline
                                C Offline
                                Cadimi
                                wrote on last edited by
                                #15

                                I tried a simple example, its value at that time is 1

                                D 1 Reply Last reply
                                0
                                • C Cadimi

                                  I tried a simple example, its value at that time is 1

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  Ok, what do the two subsequent calls to fwrite() return?

                                  "Love people and use things, not love things and use people." - Unknown

                                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                  C 1 Reply Last reply
                                  0
                                  • D David Crow

                                    Ok, what do the two subsequent calls to fwrite() return?

                                    "Love people and use things, not love things and use people." - Unknown

                                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                    C Offline
                                    C Offline
                                    Cadimi
                                    wrote on last edited by
                                    #17

                                    I just watch the file pointer, it increase the value equal to the size I've requested to write on file

                                    D 1 Reply Last reply
                                    0
                                    • C Cadimi

                                      I just watch the file pointer, it increase the value equal to the size I've requested to write on file

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #18

                                      But what do the two subsequent calls to fwrite() return?

                                      "Love people and use things, not love things and use people." - Unknown

                                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                      C 1 Reply Last reply
                                      0
                                      • D David Crow

                                        But what do the two subsequent calls to fwrite() return?

                                        "Love people and use things, not love things and use people." - Unknown

                                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                        C Offline
                                        C Offline
                                        Cadimi
                                        wrote on last edited by
                                        #19

                                        I've found my problem :D... It happened when I ust CFileDialog in another code, select a path, then the default directory change too => write file at another place => can't read. Thanks for your help

                                        D 1 Reply Last reply
                                        0
                                        • C Cadimi

                                          I've found my problem :D... It happened when I ust CFileDialog in another code, select a path, then the default directory change too => write file at another place => can't read. Thanks for your help

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #20

                                          Cadimi wrote:

                                          ...can't read.

                                          Which is why I was asking if fopen() was failing.

                                          "Love people and use things, not love things and use people." - Unknown

                                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                          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