buffer problems with CFile
-
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 correctlyfPic.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.
-
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 correctlyfPic.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 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:)
-
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 correctlyfPic.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.
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).
-
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 correctlyfPic.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.
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;
}
-
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;
}
You're first writing a
CONFIGFILE_HEADER
record to the file, rewinding it, and then writing one or moreCONFIGFILE_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
-
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 correctlyfPic.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.
-
Your ret is set to false and the return value from the file open is returned but never used in your main function.