how to save variables of type cString/int/bool in a file? 
Author Message
 how to save variables of type cString/int/bool in a file?

Hello,

I want to save some data(variables: cString/int/bool) in a file.

why doesn't this work?

     HANDLE hSetupFile;
     DWORD cbWritten;

     hSetupFile = CreateFile("AltoniT.cfg",
          GENERIC_WRITE,
          FILE_SHARE_READ,
          NULL,
          CREATE_ALWAYS,
          FILE_ATTRIBUTE_NORMAL,
          NULL);

 if (hSetupFile == INVALID_HANDLE_VALUE)
 {
        AfxMessageBox("Error opening AltiniT.cfg");  // process
error_status_tend
 }

 WriteFile(hSetupFile,&m_szLabel1,25,&cbWritten,NULL);
 WriteFile(hSetupFile,&m_nTemp1,sizeof(int),&cbWritten,NULL);
 WriteFile(hSetupFile,&m_nDif1,sizeof(int),&cbWritten,NULL);
 WriteFile(hSetupFile,&m_bAlarm1,sizeof(BOOL),&cbWritten,NULL);
...

 HANDLE hSetupFile;
 DWORD cbWritten;

 hSetupFile = CreateFile("AltoniT.cfg",
      GENERIC_READ,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      NULL);

 if (hSetupFile == INVALID_HANDLE_VALUE)
 {
        AfxMessageBox("Error opening AltiniT.cfg");  // process
error_status_tend
 }

 ReadFile(hSetupFile,&m_szLabel1,25,&cbWritten,NULL);
 ReadFile(hSetupFile,&m_nTemp1,sizeof(int),&cbWritten,NULL);
 ReadFile(hSetupFile,&m_nDif1,sizeof(int),&cbWritten,NULL);
 ReadFile(hSetupFile,&m_bAlarm1,sizeof(BOOL),&cbWritten,NULL);

 ReadFile(hSetupFile,&m_szLabel2,25,&cbWritten,NULL);
 ReadFile(hSetupFile,&m_nTemp2,sizeof(int),&cbWritten,NULL);
 ReadFile(hSetupFile,&m_nDif2,sizeof(int),&cbWritten,NULL);
 ReadFile(hSetupFile,&m_bAlarm2,sizeof(BOOL),&cbWritten,NULL);
...
Thanx for your help.



Sat, 25 Oct 2003 16:05:39 GMT  
 how to save variables of type cString/int/bool in a file?
It sure would help to know what "doesn't work" means in this case.


Sun, 26 Oct 2003 09:14:43 GMT  
 how to save variables of type cString/int/bool in a file?
Hullo Jan,

what you're actually doing is storing and then loading the internal administration data of a CString object. That's nonsense, of course.
For what you want are the characters, right?

So don't use &m_szLabel1 in

Quote:
> WriteFile(hSetupFile,&m_szLabel1,25,&cbWritten,NULL);

but something like

   WriteFile(hSetupFile, m_szLabel1.GetBuffer(25), 25, &cbWritten,NULL);
   m_szLabel1.ReleaseBuffer();

(assuming that m_szLabel1 is a CString).

Dirk Meier

_______________________________________________
Submitted via WebNewsReader of http://www.interbulletin.com



Sun, 26 Oct 2003 15:56:56 GMT  
 how to save variables of type cString/int/bool in a file?
I would NOT assume that a variable named m_szAnything is a CString!  The sz
prefix means "string, zero-terminated".  It is much more common for such a
variable to be a char[].  The standard prefixes for CString or string objects is
"str" or "cs".

Also, there is no need for the GetBuffer, ReleaseBuffer stuff when calling
WriteFile.  If it were a CString, calling the LPCTSTR cast operator would be
more appropriate.

        WriteFile(hSetupFile, (LPCTSTR)m_strLabel1, 25, &cbWritten, NULL);



Sun, 26 Oct 2003 17:40:21 GMT  
 how to save variables of type cString/int/bool in a file?
Assuming your m_szLabel1 variable is a char[], do not use the address-of
operator when writing it.  The name of the array is already treated like a
pointer:
        WriteFile(hSetupFile, m_szLabel1, 25, &cbWritten, NULL);


Sun, 26 Oct 2003 17:42:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. CString - string / BOOL - bool

2. Why use BOOL versus native data type bool?

3. CString to int and int to CString

4. RTF saving and int to CString conversion

5. CString and int variable

6. Converting from CString to type int??? Help

7. Type conversions from CString or char[] to int

8. How can I convert CString to int type

9. How to save a HTML file int txt format from the CHtmlView class

10. Save a CString into a file

11. How to save a CString in struct to a File

12. Variables and types of type "Type"

 

 
Powered by phpBB® Forum Software