From: Eddie Paz on 26 Feb 2010 08:02 First, you don't need to copy it to a temp variable. Second, your telling CFile to write the "size" of szSampleText which will write your text, but the empty space will be written and shown as squares. You need to pass the length of the string to be written. Try this: char szSampleText[] = "Your text"; cfile_object.Write(szSampleText, strlen(szSampleText)); You could also use CStdioFile which has a WriteString function that makes it easy and it also takes a CString. Eddie. "Dolf Mardan" wrote in message news:201022514127dolfakar(a)hotmail.com... > Dear All, > I need to use file read/write in VC++ MFC, I used the following code to > write to a txt file, but I got squares instead of text "Sample Text for > CFile Write function Example": > here is the code I used: > ------ > CFile cfile_object; > cfile_object.Open( "c:\\test\\ucancode_cfile_example.dat", > CFile::modeCreate|CFile::modeWrite); > > char szSampleText[256]; > strcpy(szSampleText, "Sample Text for CFile Write function Example"); > cfile_object.Write (szSampleText, sizeof (szSampleText)); > -------------- > > please advice. > > Dolf
From: Giovanni Dicanio on 26 Feb 2010 11:59 "Eddie Paz" <drpazz(a)hotmail.com> ha scritto nel messaggio news:eLP7LQutKHA.3656(a)TK2MSFTNGP06.phx.gbl... > You could also use CStdioFile which has a WriteString function that makes > it easy and it also takes a CString. CStdioFile seems to me good only for ANSI text, but buggy and so unsuitable for Unicode text. In fact, in Unicode builds, CStdioFile does not write Unicode BOM to text files, and does not write correct Unicode text at all. Instead, the aforementioned CStdioFileEx works fine for Unicode (in fact, CStdioFileEx correctly writes UTF-16 BOM, and correct Unicode UTF-16 text as well. http://www.codeproject.com/KB/files/stdiofileex.aspx The following MFC test code shows the problems with CStdioFile: <code> void CTestFileExDlg::OnBnClickedButton1() { CString filename( L"C:\\TEMP\\test.txt" ); // Enable the following #define to test code with CStdioFileEx //#define USE_CSTDIOFILEEX // // Try creating a file to write Unicode text. // #if defined(USE_CSTDIOFILEEX) CString fileClassName( L"CStdioFileEx"); CStdioFileEx file; if ( file.Open( filename, CFile::modeCreate | CFile::modeWrite | CStdioFileEx::modeWriteUnicode ) ) #else CString fileClassName( L"CStdioFile"); CStdioFile file; if ( file.Open( filename, CFile::modeCreate | CFile::modeWrite ) ) #endif // defined(USE_CSTDIOFILEEX) { CString msg; msg.Format(L"Writing Unicode text to file %s using class %s.", filename.GetString(), fileClassName.GetString()); AfxMessageBox(msg); // // File opened successfully. // Write some text to it. // file.WriteString(L"This is a Unicode text file.\r\n"); file.WriteString(L"Copyright sign: \u00A9\r\n"); file.WriteString(L"Latin Small letter e with acute: \u00E9\r\n"); file.WriteString(L"Unicode square root: \u221A\r\n"); file.WriteString(L"Katakana letter u: \u30A6\r\n"); } else { // // Error // CString msg; msg.Format(L"Error in opening file %s.", filename.GetString()); AfxMessageBox(msg, MB_ICONERROR); } // No need to explicitly call file.Close() thanks to C++ RAII pattern. } </code> I prefer waiting confirmation or negation by some experts in the field like Mihai, but I'm pretty sure that CStdioFile does not work correctly for Unicode. Giovanni
From: Giovanni Dicanio on 26 Feb 2010 13:10 "Giovanni Dicanio" <giovanniDOTdicanio(a)REMOVEMEgmail.com> ha scritto nel messaggio news:e2DkUUwtKHA.5148(a)TK2MSFTNGP04.phx.gbl... > CStdioFile seems to me good only for ANSI text, but buggy and so > unsuitable for Unicode text. https://connect.microsoft.com/VisualStudio/feedback/details/536905/cstdiofile-mfc-class-does-not-write-unicode-text-correctly Giovanni
First
|
Prev
|
Pages: 1 2 Prev: Windows 7 and MFC Development Next: Excel automation to an already open XLS |