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. buffer problems with CFile

buffer problems with CFile

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 5 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.
  • K Offline
    K Offline
    Krauze
    wrote on last edited by
    #1

    This class is used to save the pix paths in the ListBox into a file. CListItemData is a class that stores additional info for every item in ListBox. This class operates correctly.

    void CCfgFile::Serialize( const CListItemData& lid, const CListBox& lst )
    {
    loadConfigFile(); // a private funx which loads the file, it works correctly

    fPic.Seek( sizeof( CONFIGFILE\_HEADER ), CFile::begin );
        // ommit the header section and move to the data
    
    CString str;
    int i = 0;
    while( i < lst.GetCount() )
    {
    	// get the pic full path by combining the data in CListItemData and CListBox
    	lst.GetText( i, str );
    	str = lid.GetDir(i) + \_T("\\\\") + str + \_T(".") + lid.GetType(i);
    
    	// initialize rcd, a structure for the file data
    	memset( rcd.strPath, 0, sizeof( rcd.strPath ) );
    	memset( rcd.nStyle, 0, sizeof( rcd.nStyle ) );
    	
                // write the data into rcd structure
    	strcpy( rcd.strPath, str );
    	itoa( lid.GetStyle(i), rcd.nStyle, 10 );
    	rcd.nStyle\[1\] = '\\0';
            
                // write the structure into file
    	fPic.Write( &rcd, sizeof( CONFIGFILE\_RECORD ) );
    	
                i++;
    	
    }
    
    fPic.Close();
    

    }

    The prob occurs. Sometimes the file buffer is updated and the ListBox can show the updated data. But after I exit the proram, the disk file is not updated. This really confuses me. Even if I use CFile::Flush(), it's the same. Anyone could help me out? Really thanx.

    H I K V 4 Replies Last reply
    0
    • K Krauze

      This class is used to save the pix paths in the ListBox into a file. CListItemData is a class that stores additional info for every item in ListBox. This class operates correctly.

      void CCfgFile::Serialize( const CListItemData& lid, const CListBox& lst )
      {
      loadConfigFile(); // a private funx which loads the file, it works correctly

      fPic.Seek( sizeof( CONFIGFILE\_HEADER ), CFile::begin );
          // ommit the header section and move to the data
      
      CString str;
      int i = 0;
      while( i < lst.GetCount() )
      {
      	// get the pic full path by combining the data in CListItemData and CListBox
      	lst.GetText( i, str );
      	str = lid.GetDir(i) + \_T("\\\\") + str + \_T(".") + lid.GetType(i);
      
      	// initialize rcd, a structure for the file data
      	memset( rcd.strPath, 0, sizeof( rcd.strPath ) );
      	memset( rcd.nStyle, 0, sizeof( rcd.nStyle ) );
      	
                  // write the data into rcd structure
      	strcpy( rcd.strPath, str );
      	itoa( lid.GetStyle(i), rcd.nStyle, 10 );
      	rcd.nStyle\[1\] = '\\0';
              
                  // write the structure into file
      	fPic.Write( &rcd, sizeof( CONFIGFILE\_RECORD ) );
      	
                  i++;
      	
      }
      
      fPic.Close();
      

      }

      The prob occurs. Sometimes the file buffer is updated and the ListBox can show the updated data. But after I exit the proram, the disk file is not updated. This really confuses me. Even if I use CFile::Flush(), it's the same. Anyone could help me out? Really thanx.

      H Offline
      H Offline
      hanq_38910130
      wrote on last edited by
      #2

      I think there is not enough information about your problem in your post, so can you just provide some more information? Like definition of the struct type of "rcd"? Besides, maybe you can try to debug the application to find the reason of your problem:)

      1 Reply Last reply
      0
      • K Krauze

        This class is used to save the pix paths in the ListBox into a file. CListItemData is a class that stores additional info for every item in ListBox. This class operates correctly.

        void CCfgFile::Serialize( const CListItemData& lid, const CListBox& lst )
        {
        loadConfigFile(); // a private funx which loads the file, it works correctly

        fPic.Seek( sizeof( CONFIGFILE\_HEADER ), CFile::begin );
            // ommit the header section and move to the data
        
        CString str;
        int i = 0;
        while( i < lst.GetCount() )
        {
        	// get the pic full path by combining the data in CListItemData and CListBox
        	lst.GetText( i, str );
        	str = lid.GetDir(i) + \_T("\\\\") + str + \_T(".") + lid.GetType(i);
        
        	// initialize rcd, a structure for the file data
        	memset( rcd.strPath, 0, sizeof( rcd.strPath ) );
        	memset( rcd.nStyle, 0, sizeof( rcd.nStyle ) );
        	
                    // write the data into rcd structure
        	strcpy( rcd.strPath, str );
        	itoa( lid.GetStyle(i), rcd.nStyle, 10 );
        	rcd.nStyle\[1\] = '\\0';
                
                    // write the structure into file
        	fPic.Write( &rcd, sizeof( CONFIGFILE\_RECORD ) );
        	
                    i++;
        	
        }
        
        fPic.Close();
        

        }

        The prob occurs. Sometimes the file buffer is updated and the ListBox can show the updated data. But after I exit the proram, the disk file is not updated. This really confuses me. Even if I use CFile::Flush(), it's the same. Anyone could help me out? Really thanx.

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        What is fPic? That's a pretty crucial bit of missing data... How is the file opened? Could it be failing on the opening (ie, already open elsewhere) and you're not handling the errors from a failed opening? Iain.

        I have now moved to Sweden for love (awwww).

        1 Reply Last reply
        0
        • K Krauze

          This class is used to save the pix paths in the ListBox into a file. CListItemData is a class that stores additional info for every item in ListBox. This class operates correctly.

          void CCfgFile::Serialize( const CListItemData& lid, const CListBox& lst )
          {
          loadConfigFile(); // a private funx which loads the file, it works correctly

          fPic.Seek( sizeof( CONFIGFILE\_HEADER ), CFile::begin );
              // ommit the header section and move to the data
          
          CString str;
          int i = 0;
          while( i < lst.GetCount() )
          {
          	// get the pic full path by combining the data in CListItemData and CListBox
          	lst.GetText( i, str );
          	str = lid.GetDir(i) + \_T("\\\\") + str + \_T(".") + lid.GetType(i);
          
          	// initialize rcd, a structure for the file data
          	memset( rcd.strPath, 0, sizeof( rcd.strPath ) );
          	memset( rcd.nStyle, 0, sizeof( rcd.nStyle ) );
          	
                      // write the data into rcd structure
          	strcpy( rcd.strPath, str );
          	itoa( lid.GetStyle(i), rcd.nStyle, 10 );
          	rcd.nStyle\[1\] = '\\0';
                  
                      // write the structure into file
          	fPic.Write( &rcd, sizeof( CONFIGFILE\_RECORD ) );
          	
                      i++;
          	
          }
          
          fPic.Close();
          

          }

          The prob occurs. Sometimes the file buffer is updated and the ListBox can show the updated data. But after I exit the proram, the disk file is not updated. This really confuses me. Even if I use CFile::Flush(), it's the same. Anyone could help me out? Really thanx.

          K Offline
          K Offline
          Krauze
          wrote on last edited by
          #4

          struct CONFIGFILE_RECORD
          {
          char strPath[MAX_PATH]; // the path of the pic
          char nStyle[2]; // nStyle==0, IPIC_CENTER
          // nStyle==1, IPIC_TILE
          // nStyle==2, IPIC_STRETCH
          }; // the record item of the config file

          	// one more byte for the null terminator
          	struct CONFIGFILE\_HEADER
          	{
          		char bHasPic\[2\];  // bHasPic==0, no pic paths are in the file
          				  // bHasPic==1
          		char bOrder\[2\];   // bOrder==1, the wallpaper is changed according to the set order
          			          // bOrder==0, randomly
          		char nTime\[5\];    // the interval for wallpaper changing
          				  // max: 9000s = 2.5h
          		char bEffect\[2\];  // bEffect==0, no special effect is used when the wallpaper is changing
          				  // bEffect==1
          
          		char nNum\[3\];    // the number of pix
          
          		char nPos\[3\];    // the last position in the file that indicates a particular pic path
          				 // 3 means this program supports up to 99 pics
          	}; // the header of the configuration file
          

          BOOL CCfgFile::loadConfigFile()
          {
          BOOL ret = FALSE;

          // get the current dir
          char dir\[MAX\_PATH\];
          ::GetCurrentDirectory( MAX\_PATH, dir );
          strcat( dir, "\\\\config" );
          
          UINT mode = CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyNone;
          if( fPic.m\_hFile )
          	fPic.Close();
          ret = fPic.Open( dir, mode );
          
          if( !fPic.GetLength() )
          {
          	strcpy( hd.bHasPic, \_T("0") );
          	strcpy( hd.bOrder, \_T("1") );
          	strcpy( hd.nTime, \_T("9999") );
          	strcpy( hd.bEffect, \_T("0") );
          	strcpy( hd.nNum, \_T("0") );
          	strcpy( hd.nPos, \_T("0") );
          	fPic.Write( &hd, sizeof( CONFIGFILE\_HEADER ) );
          } // some initializations if the file is not exised and created
          
          return ret;
          

          }

          D 1 Reply Last reply
          0
          • K Krauze

            struct CONFIGFILE_RECORD
            {
            char strPath[MAX_PATH]; // the path of the pic
            char nStyle[2]; // nStyle==0, IPIC_CENTER
            // nStyle==1, IPIC_TILE
            // nStyle==2, IPIC_STRETCH
            }; // the record item of the config file

            	// one more byte for the null terminator
            	struct CONFIGFILE\_HEADER
            	{
            		char bHasPic\[2\];  // bHasPic==0, no pic paths are in the file
            				  // bHasPic==1
            		char bOrder\[2\];   // bOrder==1, the wallpaper is changed according to the set order
            			          // bOrder==0, randomly
            		char nTime\[5\];    // the interval for wallpaper changing
            				  // max: 9000s = 2.5h
            		char bEffect\[2\];  // bEffect==0, no special effect is used when the wallpaper is changing
            				  // bEffect==1
            
            		char nNum\[3\];    // the number of pix
            
            		char nPos\[3\];    // the last position in the file that indicates a particular pic path
            				 // 3 means this program supports up to 99 pics
            	}; // the header of the configuration file
            

            BOOL CCfgFile::loadConfigFile()
            {
            BOOL ret = FALSE;

            // get the current dir
            char dir\[MAX\_PATH\];
            ::GetCurrentDirectory( MAX\_PATH, dir );
            strcat( dir, "\\\\config" );
            
            UINT mode = CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyNone;
            if( fPic.m\_hFile )
            	fPic.Close();
            ret = fPic.Open( dir, mode );
            
            if( !fPic.GetLength() )
            {
            	strcpy( hd.bHasPic, \_T("0") );
            	strcpy( hd.bOrder, \_T("1") );
            	strcpy( hd.nTime, \_T("9999") );
            	strcpy( hd.bEffect, \_T("0") );
            	strcpy( hd.nNum, \_T("0") );
            	strcpy( hd.nPos, \_T("0") );
            	fPic.Write( &hd, sizeof( CONFIGFILE\_HEADER ) );
            } // some initializations if the file is not exised and created
            
            return ret;
            

            }

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

            You're first writing a CONFIGFILE_HEADER record to the file, rewinding it, and then writing one or more CONFIGFILE_RECORD records to it. :confused: Is that the intent?

            Krauze wrote:

            // get the current dir char dir[MAX_PATH]; ::GetCurrentDirectory( MAX_PATH, dir ); strcat( dir, "\\config" );

            This will not always result in the same path.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Man who follows car will be exhausted." - Confucius

            1 Reply Last reply
            0
            • K Krauze

              This class is used to save the pix paths in the ListBox into a file. CListItemData is a class that stores additional info for every item in ListBox. This class operates correctly.

              void CCfgFile::Serialize( const CListItemData& lid, const CListBox& lst )
              {
              loadConfigFile(); // a private funx which loads the file, it works correctly

              fPic.Seek( sizeof( CONFIGFILE\_HEADER ), CFile::begin );
                  // ommit the header section and move to the data
              
              CString str;
              int i = 0;
              while( i < lst.GetCount() )
              {
              	// get the pic full path by combining the data in CListItemData and CListBox
              	lst.GetText( i, str );
              	str = lid.GetDir(i) + \_T("\\\\") + str + \_T(".") + lid.GetType(i);
              
              	// initialize rcd, a structure for the file data
              	memset( rcd.strPath, 0, sizeof( rcd.strPath ) );
              	memset( rcd.nStyle, 0, sizeof( rcd.nStyle ) );
              	
                          // write the data into rcd structure
              	strcpy( rcd.strPath, str );
              	itoa( lid.GetStyle(i), rcd.nStyle, 10 );
              	rcd.nStyle\[1\] = '\\0';
                      
                          // write the structure into file
              	fPic.Write( &rcd, sizeof( CONFIGFILE\_RECORD ) );
              	
                          i++;
              	
              }
              
              fPic.Close();
              

              }

              The prob occurs. Sometimes the file buffer is updated and the ListBox can show the updated data. But after I exit the proram, the disk file is not updated. This really confuses me. Even if I use CFile::Flush(), it's the same. Anyone could help me out? Really thanx.

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              Your ret is set to false and the return value from the file open is returned but never used in your main function.

              K 1 Reply Last reply
              0
              • V Vaclav_

                Your ret is set to false and the return value from the file open is returned but never used in your main function.

                K Offline
                K Offline
                Krauze
                wrote on last edited by
                #7

                Thank y'all! I've got it solved with the help of DavidCrow. Actually, ::GetCurrentDirectory() may return inconsistent paths.

                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