From: Dolf on 25 Feb 2010 14:12 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 Guido Franzke wrote: Re: strcpy problem 05-Feb-07 You must cast the std:string to const char*, the following way: std::string source = "Hello world"; char sz[1000]; strcpy(sz, source.c_str()); Same with *Iter. Regards, Guido Previous Posts In This Thread: On Monday, February 05, 2007 5:51 AM lencastro wrote: strcpy problem typedef std::list<std::basic_string<TCHAR> > str_list; .. .. .. str_list::iterator Iter .. .. .. strcpy(temp,*Iter); ________________________________________________ When i tried to use the above code... i got the following error... error C2664: 'strcpy' : cannot convert parameter 2 from 'class std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::iterator' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Plz explain the code also the error...in detail Thank you On Monday, February 05, 2007 6:38 AM Guido Franzke wrote: Re: strcpy problem You must cast the std:string to const char*, the following way: std::string source = "Hello world"; char sz[1000]; strcpy(sz, source.c_str()); Same with *Iter. Regards, Guido On Monday, February 05, 2007 9:48 AM Ajay Kalra wrote: Re: strcpy problem Since you are using MFC, why not use CString/CStringArray? --- Ajay On Monday, February 05, 2007 12:28 PM Joseph M. Newcomer wrote: Re: strcpy problem Several things: First, why are you using strcpy when you have str_list defined in terms of TCHAR? Second, why do you think *Iter is the address of the string? It is the address of a std::basic_string<TCHAR>, but that is not the text of the string required by strcpy. Try _tcscpy(temp, (*iter).c_str()); or something very similar to that (I don't use std:: so I'm not sure about the (*iter), but the c_str() *is* required as far as I know). And, more seriously, why are you using strcpy and a character array anyway? These should be totally unnecessary when using CString or std::string. The first thing to learn about C++ programming, whether you use CString or std::string, is that the notion of character arrays is largely dead, and is needed only in rare and exotic circumstances. And the char type is definitely obsolete except in rare and exotic circumstances. You have not explained your code well enough to justify that you have a rare and/or exotic circumstance. joe On 5 Feb 2007 02:51:41 -0800, "lencastro" <lencastro(a)gmail.com> wrote: Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm On Tuesday, February 06, 2007 3:51 AM lencastro wrote: Re: strcpy problem Thanks Mr.Joseph I have solved the problem. your post helped me in many ways. Submitted via EggHeadCafe - Software Developer Portal of Choice Dr. Dotnetsky's Cool .Net Tips and Tricks No. 26 http://www.eggheadcafe.com/tutorials/aspnet/8e390106-20d1-4b2b-be30-cde5d852348d/dr-dotnetskys-cool-net.aspx
From: Joseph M. Newcomer on 25 Feb 2010 16:04 See below... On Thu, 25 Feb 2010 11:12:20 -0800, Dolf Mardan wrote: > >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"); ***** I am curious why you felt it was necessary to declare a buffer of fixed size, or to copy a string into using strcpy. It would be trivial to have written LPSTR SampleText = "Sample text for CFile Write function example"; which doesn't involve gratuitous copy operations. I have no idea what you mean by "got squares". This doesn't even make sense. What you *might* have meant was that you read the file into some program (undefined) and it showed squares, but unless you say something about how you derived this information about "got squares" we can't help much. You might also consider using CStdioFile. You also did not show the cfile_object.Close(); statement; if you somehow missed doing that, then it is not surprising that the file might not represent any form of reality you would like. joe **** >cfile_object.Write (szSampleText, sizeof (szSampleText)); **** Note that you are going to get a lot of garbage bytes, because you didn't ask it it write out the contents of the string, you asked it to write out the contents of the entire buffer! sizeof() (which is in general erroneous usage) is completely inappropriate here. It should be cfile_object.Write(szSampleText, strlen(szSampleText)); the code you wrote will write out 256 bytes no matter how long the string is. The CORRECT approach would be cfile_object.Write(szSampleText. _tcslen(szSampleText) * sizeof(TCHAR) ); because you should not be using the "char" data type at all. If you are writing a CString, it would be CString szSampleText = _T("..."); cfile_object.Write(szSampleText, szSampleText.GetLength() * sizeof(TCHAR)); you should avoid the use of std::string in MFC programs; it adds nothing, and simply adds to the complexity because it is not a good match for MFC; the built-in CString operators will simplify your coding. joe **** >-------------- > >please advice. > >Dolf > > > >Guido Franzke wrote: > >Re: strcpy problem >05-Feb-07 > >You must cast the std:string to const char*, the following way: > >std::string source = "Hello world"; >char sz[1000]; > >strcpy(sz, source.c_str()); > >Same with *Iter. >Regards, Guido > >Previous Posts In This Thread: > >On Monday, February 05, 2007 5:51 AM >lencastro wrote: > >strcpy problem >typedef std::list<std::basic_string<TCHAR> > str_list; >.. >.. >.. >str_list::iterator Iter >.. >.. >.. >strcpy(temp,*Iter); >________________________________________________ >When i tried to use the above code... >i got the following error... > >error C2664: 'strcpy' : cannot convert parameter 2 from 'class >std::list<class std::basic_string<char,struct >std::char_traits<char>,class std::allocator<char> >,class >std::allocator<class std::basic_string<char,struct >std::char_traits<char>,class std::allocator<char> > > >::iterator' to >'const char *' > > No user-defined-conversion operator available that can perform >this conversion, or the operator cannot be called > >Plz explain the code also the error...in detail > >Thank you > >On Monday, February 05, 2007 6:38 AM >Guido Franzke wrote: > >Re: strcpy problem >You must cast the std:string to const char*, the following way: > >std::string source = "Hello world"; >char sz[1000]; > >strcpy(sz, source.c_str()); > >Same with *Iter. >Regards, Guido > >On Monday, February 05, 2007 9:48 AM >Ajay Kalra wrote: > >Re: strcpy problem >Since you are using MFC, why not use CString/CStringArray? > >--- >Ajay > >On Monday, February 05, 2007 12:28 PM >Joseph M. Newcomer wrote: > >Re: strcpy problem >Several things: > >First, why are you using strcpy when you have str_list defined in terms of TCHAR? > >Second, why do you think *Iter is the address of the string? It is the address of a >std::basic_string<TCHAR>, but that is not the text of the string required by strcpy. > >Try > > _tcscpy(temp, (*iter).c_str()); >or something very similar to that (I don't use std:: so I'm not sure about the (*iter), >but the c_str() *is* required as far as I know). > >And, more seriously, why are you using strcpy and a character array anyway? These should >be totally unnecessary when using CString or std::string. > >The first thing to learn about C++ programming, whether you use CString or std::string, is >that the notion of character arrays is largely dead, and is needed only in rare and exotic >circumstances. And the char type is definitely obsolete except in rare and exotic >circumstances. You have not explained your code well enough to justify that you have a >rare and/or exotic circumstance. > joe > > >On 5 Feb 2007 02:51:41 -0800, "lencastro" <lencastro(a)gmail.com> wrote: > >Joseph M. Newcomer [MVP] >email: newcomer(a)flounder.com >Web: http://www.flounder.com >MVP Tips: http://www.flounder.com/mvp_tips.htm > >On Tuesday, February 06, 2007 3:51 AM >lencastro wrote: > >Re: strcpy problem >Thanks Mr.Joseph >I have solved the problem. >your post helped me in many ways. > > >Submitted via EggHeadCafe - Software Developer Portal of Choice >Dr. Dotnetsky's Cool .Net Tips and Tricks No. 26 >http://www.eggheadcafe.com/tutorials/aspnet/8e390106-20d1-4b2b-be30-cde5d852348d/dr-dotnetskys-cool-net.aspx Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Tom Serface on 26 Feb 2010 01:40 The squares probably indicate a problem with Unicode characters being written out as chars. Is your project compiled as Unicode. If so you may want to use TCHAR to declare your buffer and tcscpy() to do the copy, although you don't really need to do a string copy to do what you want. You may want to save yourself a lot of trouble and just use a CString to hold your string. Since you are using CFile you already have MFC so using CString is a lot more convenient and it already compiles ANSI or Unicode depending on the compile defines. Tom "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 > > > > Guido Franzke wrote: > > Re: strcpy problem > 05-Feb-07 > > You must cast the std:string to const char*, the following way: > > std::string source = "Hello world"; > char sz[1000]; > > strcpy(sz, source.c_str()); > > Same with *Iter. > Regards, Guido > > Previous Posts In This Thread: > > On Monday, February 05, 2007 5:51 AM > lencastro wrote: > > strcpy problem > typedef std::list<std::basic_string<TCHAR> > str_list; > . > . > . > str_list::iterator Iter > . > . > . > strcpy(temp,*Iter); > ________________________________________________ > When i tried to use the above code... > i got the following error... > > error C2664: 'strcpy' : cannot convert parameter 2 from 'class > std::list<class std::basic_string<char,struct > std::char_traits<char>,class std::allocator<char> >,class > std::allocator<class std::basic_string<char,struct > std::char_traits<char>,class std::allocator<char> > > >::iterator' to > 'const char *' > > No user-defined-conversion operator available that can perform > this conversion, or the operator cannot be called > > Plz explain the code also the error...in detail > > Thank you > > On Monday, February 05, 2007 6:38 AM > Guido Franzke wrote: > > Re: strcpy problem > You must cast the std:string to const char*, the following way: > > std::string source = "Hello world"; > char sz[1000]; > > strcpy(sz, source.c_str()); > > Same with *Iter. > Regards, Guido > > On Monday, February 05, 2007 9:48 AM > Ajay Kalra wrote: > > Re: strcpy problem > Since you are using MFC, why not use CString/CStringArray? > > --- > Ajay > > On Monday, February 05, 2007 12:28 PM > Joseph M. Newcomer wrote: > > Re: strcpy problem > Several things: > > First, why are you using strcpy when you have str_list defined in terms of > TCHAR? > > Second, why do you think *Iter is the address of the string? It is the > address of a > std::basic_string<TCHAR>, but that is not the text of the string required > by strcpy. > > Try > > _tcscpy(temp, (*iter).c_str()); > or something very similar to that (I don't use std:: so I'm not sure about > the (*iter), > but the c_str() *is* required as far as I know). > > And, more seriously, why are you using strcpy and a character array > anyway? These should > be totally unnecessary when using CString or std::string. > > The first thing to learn about C++ programming, whether you use CString or > std::string, is > that the notion of character arrays is largely dead, and is needed only in > rare and exotic > circumstances. And the char type is definitely obsolete except in rare > and exotic > circumstances. You have not explained your code well enough to justify > that you have a > rare and/or exotic circumstance. > joe > > > On 5 Feb 2007 02:51:41 -0800, "lencastro" <lencastro(a)gmail.com> wrote: > > Joseph M. Newcomer [MVP] > email: newcomer(a)flounder.com > Web: http://www.flounder.com > MVP Tips: http://www.flounder.com/mvp_tips.htm > > On Tuesday, February 06, 2007 3:51 AM > lencastro wrote: > > Re: strcpy problem > Thanks Mr.Joseph > I have solved the problem. > your post helped me in many ways. > > > Submitted via EggHeadCafe - Software Developer Portal of Choice > Dr. Dotnetsky's Cool .Net Tips and Tricks No. 26 > http://www.eggheadcafe.com/tutorials/aspnet/8e390106-20d1-4b2b-be30-cde5d852348d/dr-dotnetskys-cool-net.aspx
From: Giovanni Dicanio on 26 Feb 2010 04:27 "Joseph M. Newcomer" <newcomer(a)flounder.com> ha scritto nel messaggio news:8qndo59j1a605q433tblhgp5eunsmtaoeb(a)4ax.com... > You might also consider using CStdioFile. To add to Joe's detailed post, you may want to use CStdioFileEx instead of CStdioFile: http://www.codeproject.com/KB/files/stdiofileex.aspx CStdioFile has some problems with Unicode files; CStdioFileEx manages Unicode files much better. > You also did not show the > cfile_object.Close(); > statement; if you somehow missed doing that, then it is not surprising > that the file might > not represent any form of reality you would like. Joe: I don't agree with you on that point (or probably I'm missing something...). In fact, CFile uses the RAII pattern, so the destructor will close the file also if OP did not explicitly call Close(). Giovanni
From: Giovanni Dicanio on 26 Feb 2010 04:49 "Dolf Mardan" ha scritto nel messaggio news:201022514127dolfakar(a)hotmail.com... > 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: > [...] As already written, you may want to use CStdioFileEx class to manage Unicode text files in MFC (consider ANSI something obsolete of the past): http://www.codeproject.com/KB/files/stdiofileex.aspx You can use C++/MFC code like this to write some text to the file (verified with VS2008 SP1, Unicode build): <code> void CTestFileExDlg::OnBnClickedButton1() { // // Try creating a file to write Unicode text. // CString filename( L"C:\\TEMP\\test.txt" ); CStdioFileEx file; if ( file.Open( filename, CFile::modeCreate | CFile::modeWrite | CStdioFileEx::modeWriteUnicode ) ) { // // File opened successfully. // Write some text to it. // file.WriteString(L"This is a Unicode text file.\n"); file.WriteString(L"Copyright sign: \u00A9\n"); file.WriteString(L"Latin Small letter e with acute: \u00E9\n"); } // No need to explicitly call file.Close() thanks to C++ RAII pattern. } </code> The output file will look like this: <output> This is a Unicode text file. Copyright sign: � Latin Small letter e with acute: � </output> HTH, Giovanni
|
Next
|
Last
Pages: 1 2 Prev: Windows 7 and MFC Development Next: Excel automation to an already open XLS |