Hello,

I've a problem writing a CString to file.
I first convert to char* but it does not seem to work.
The code is below ...

Bob


void Test::WriteLog(char* path,CString message)
{
char* text = new char[1024];
text = (char*)message.GetBuffer(sizeof(text));
FILE* fp = fopen(path,"a");
fprintf(fp,"\n%c",text);
fclose(fp);
delete text;
}

Re: write CString to file by bob

bob
Tue Jul 29 06:25:12 CDT 2003

Hello Axel,

The "c" is a typo. Sorry. I do use fprintf(fp,"\n%s",text) but it does not
work.
The problem is the conversion from CString to char*.
I checked the CString and it is OK.

Bob


"Axel Richter" <Axel.Richter@freenet.de> wrote in message
news:3F265849.5000009@freenet.de...
> bob schrieb:
> > Hello,
> >
> > I've a problem writing a CString to file.
> > I first convert to char* but it does not seem to work.
> > The code is below ...
> >
> > Bob
> >
> >
> > void Test::WriteLog(char* path,CString message)
> > {
> > char* text = new char[1024];
> > text = (char*)message.GetBuffer(sizeof(text));
> > FILE* fp = fopen(path,"a");
> > fprintf(fp,"\n%c",text);
> > fclose(fp);
> > delete text;
> > }
> >
> >
>
> Hi bob,
>
> try:
>
> fprintf(fp,"\n%s",text);
>
> instead of
>
> fprintf(fp,"\n%c",text);
>
> Axel.
>



Re: write CString to file by Creative

Creative
Tue Jul 29 06:29:55 CDT 2003

There are a number of things wrong with this code...

In article <OMysG#bVDHA.1512@TK2MSFTNGP11.phx.gbl>, lbortels@vub.ac.be
says...
> Hello,
>
> I've a problem writing a CString to file.
> I first convert to char* but it does not seem to work.
> The code is below ...
>
> Bob
>
>
> void Test::WriteLog(char* path,CString message)
> {
> char* text = new char[1024];
> text = (char*)message.GetBuffer(sizeof(text));

At this point, you've lost the memory allocated by new and pointed to by
text after it was initialized. Variable text now points to a buffer
owned by message which, depending on the original length of message,
might be new and uninitialized.

> FILE* fp = fopen(path,"a");
> fprintf(fp,"\n%c",text);

Here, you are:
a) Using %c (which expects a character as an int) when you probably
meant %s.
b) Using message's possibly uninitialized buffer.

> fclose(fp);
> delete text;

Here you're deleting a buffer owned by message. Very bad.

Also, you are returning without calling ReleaseBuffer on message, which
is leaving message's buffer locked.
> }

CString::GetBuffer and the other functions that work directly with
CString's buffer are rarely needed and should generally be avoided.

If your goal is merely to print the contents of message to file path,
try this:

void Test::WriteLog(const char* path, const CString& message)
{
FILE* fp = fopen(path, "a");
fprintf(fp, "\n%s", static_cast<const char*>(message));
fclose(fp);
}

Note the use of const for the arguments, and the static_cast applied to
message. CString objects can be automatically converted to const char*,
but when calling functions like those in the printf family, which take a
varying number of arguments and are thus not type-safe, you need to tell
the compiler explicitly that you want this conversion.

Re: write CString to file by Axel

Axel
Tue Jul 29 06:46:07 CDT 2003

bob schrieb:
> Hello Axel,
>
> The "c" is a typo. Sorry. I do use fprintf(fp,"\n%s",text) but it does not
> work.
> The problem is the conversion from CString to char*.
> I checked the CString and it is OK.
>
> Bob
>
>
> "Axel Richter" <Axel.Richter@freenet.de> wrote in message
> news:3F265849.5000009@freenet.de...
>
>>bob schrieb:
>>
>>>Hello,
>>>
>>>I've a problem writing a CString to file.
>>>I first convert to char* but it does not seem to work.
>>>The code is below ...
>>>
>>>Bob
>>>
>>>
>>>void Test::WriteLog(char* path,CString message)
>>>{
>>>char* text = new char[1024];
>>>text = (char*)message.GetBuffer(sizeof(text));
>>>FILE* fp = fopen(path,"a");
>>>fprintf(fp,"\n%c",text);
>>>fclose(fp);
>>>delete text;
>>>}
>>>
>>>
>>
>>Hi bob,
>>
>>try:
>>
>>fprintf(fp,"\n%s",text);
>>
>>instead of
>>
>>fprintf(fp,"\n%c",text);
>>
>>Axel.
>>
>
>
>

Hi bob,

you should convert the CString to (const char*) to access the buffer
READONLY.

See also "Creative's" Mail for the memory usage of "text".

Axel.


Re: write CString to file by bob

bob
Tue Jul 29 07:06:43 CDT 2003

Thanx gfor the reply.
However, I get a compilation error
error 2440: 'static cast' : cannot convert from 'const class CString' to
'const char*' ...



"Creative Solutions" <q266e1d02@sneakemail.com> wrote in message
news:MPG.199024a0bbd222a9989680@news.microsoft.com...
> There are a number of things wrong with this code...
>
> In article <OMysG#bVDHA.1512@TK2MSFTNGP11.phx.gbl>, lbortels@vub.ac.be
> says...
> > Hello,
> >
> > I've a problem writing a CString to file.
> > I first convert to char* but it does not seem to work.
> > The code is below ...
> >
> > Bob
> >
> >
> > void Test::WriteLog(char* path,CString message)
> > {
> > char* text = new char[1024];
> > text = (char*)message.GetBuffer(sizeof(text));
>
> At this point, you've lost the memory allocated by new and pointed to by
> text after it was initialized. Variable text now points to a buffer
> owned by message which, depending on the original length of message,
> might be new and uninitialized.
>
> > FILE* fp = fopen(path,"a");
> > fprintf(fp,"\n%c",text);
>
> Here, you are:
> a) Using %c (which expects a character as an int) when you probably
> meant %s.
> b) Using message's possibly uninitialized buffer.
>
> > fclose(fp);
> > delete text;
>
> Here you're deleting a buffer owned by message. Very bad.
>
> Also, you are returning without calling ReleaseBuffer on message, which
> is leaving message's buffer locked.
> > }
>
> CString::GetBuffer and the other functions that work directly with
> CString's buffer are rarely needed and should generally be avoided.
>
> If your goal is merely to print the contents of message to file path,
> try this:
>
> void Test::WriteLog(const char* path, const CString& message)
> {
> FILE* fp = fopen(path, "a");
> fprintf(fp, "\n%s", static_cast<const char*>(message));
> fclose(fp);
> }
>
> Note the use of const for the arguments, and the static_cast applied to
> message. CString objects can be automatically converted to const char*,
> but when calling functions like those in the printf family, which take a
> varying number of arguments and are thus not type-safe, you need to tell
> the compiler explicitly that you want this conversion.



Re: write CString to file by Dan

Dan
Tue Jul 29 08:37:53 CDT 2003

In article <uU3tincVDHA.1280@tk2msftngp13.phx.gbl>, lbortels@vub.ac.be
says...
> Thanx gfor the reply.
> However, I get a compilation error
> error 2440: 'static cast' : cannot convert from 'const class CString' to
> 'const char*' ...

Hmm, static_cast works in VC7.1. Try a C-style cast: (const char*)
message. That ought to work even in VC6.

--
Dan Muller
"So that's what an invisible barrier looks like!" (Time Bandits)

Re: write CString to file by bob

bob
Tue Jul 29 09:02:57 CDT 2003

Hi Dan,

Thnax for the help but I already tried this but it gives the same problem
;-(

Bob

"Dan Muller" <q266e1d02@sneakemail.com> wrote in message
news:MPG.199042bfd9e2c89a989681@news.microsoft.com...
> In article <uU3tincVDHA.1280@tk2msftngp13.phx.gbl>, lbortels@vub.ac.be
> says...
> > Thanx gfor the reply.
> > However, I get a compilation error
> > error 2440: 'static cast' : cannot convert from 'const class CString' to
> > 'const char*' ...
>
> Hmm, static_cast works in VC7.1. Try a C-style cast: (const char*)
> message. That ought to work even in VC6.
>
> --
> Dan Muller
> "So that's what an invisible barrier looks like!" (Time Bandits)



Re: write CString to file by Rob

Rob
Tue Jul 29 09:15:57 CDT 2003

Hi Bob,

If you are using MFC, you could use CStdioFile. It has a member function
WriteString() to which you can pass a CString.

Hope that helps,
Rob



Re: write CString to file by bob

bob
Tue Jul 29 10:14:04 CDT 2003

Hi Rob,

Thanx alot! It works like a whistle :-))

Bob

"Rob" <a@b> wrote in message news:OVH3vvdVDHA.2544@tk2msftngp13.phx.gbl...
> Hi Bob,
>
> If you are using MFC, you could use CStdioFile. It has a member function
> WriteString() to which you can pass a CString.
>
> Hope that helps,
> Rob
>
>



write CString to file by Adrian

Adrian
Wed Jul 30 03:57:29 CDT 2003

One other problem that has not been mentioned is here:

text = (char*)message.GetBuffer(sizeof(text));

sizeof(text) returns the sizeof of text data type, i.e., a
pointer; all pointers in 32 bit land are 4 bytes. So
you're getting a 4 byte buffer. This should be

text = (char*)message.GetBuffer(message.GetLength());

The problem with static_cast is because the syntax is

static_cast<const char*>(message)

i.e.,

//Example A:

void Test::WriteLog(const char* path, const CString&
message)
{
FILE* fp = fopen(path, "a");
fprintf(fp, "\n%s", static_cast<const char*>(message));
fclose(fp);
}

I would prefer to write the function like this:

//Example B:

void Test::WriteLog(const char* path, const char* message)
{
FILE* fp = fopen(path, "a");
fprintf(fp, "\n%s", message);
fclose(fp);
}

This uses the CString LPCTSTR operator (e.g., const char*)
and has the advantages over example A. When you call
Example A WriteLog with a literal string, e.g.,

WriteLog("c:\\temp\\temp.log", "Hello");

a CString message object has to be created (memory
allocated). With Example B no object is created, as the
compiler just passes a pointer to the literal. This is
more efficient, particularly if you are doing a lot of
logging.

When you call Example A with a CString object, e.g.,

CString sTemp("Hello");
WriteLog("c:\\temp\\temp.log", sHello);

because message is a reference, only a pointer is passed.
With Example B, the CString operator LPCTSTR returns the
internal CString pointer to the string data. Either way is
equally as efficient.

For this reason I avoid CString& for function parameters.

After all that, in MFC you should be using CStdioFile.



>-----Original Message-----
>Hello,
>
>I've a problem writing a CString to file.
>I first convert to char* but it does not seem to work.
>The code is below ...
>
>Bob
>
>
>void Test::WriteLog(char* path,CString message)
>{
>char* text = new char[1024];
>text = (char*)message.GetBuffer(sizeof(text));
>FILE* fp = fopen(path,"a");
>fprintf(fp,"\n%c",text);
>fclose(fp);
>delete text;
>}
>
>
>.
>

write CString to file by Adrian

Adrian
Wed Jul 30 07:47:33 CDT 2003

OK, I see this message board is treating the static_cast
syntax as though it contains an HTML tag, which it is
stripping out, therefore when I post

static_cast lessthan const char* greaterthan (message)

(replace lessthan and greaterthan with the appropriate
symbols) it is appearing as

static_cast(message)

Doh!

>-----Original Message-----
>One other problem that has not been mentioned is here:
>
>text = (char*)message.GetBuffer(sizeof(text));
>
>sizeof(text) returns the sizeof of text data type, i.e.,
a
>pointer; all pointers in 32 bit land are 4 bytes. So
>you're getting a 4 byte buffer. This should be
>
>text = (char*)message.GetBuffer(message.GetLength());
>
>The problem with static_cast is because the syntax is
>
>static_cast<const char*>(message)
>
>i.e.,
>
>//Example A:
>
>void Test::WriteLog(const char* path, const CString&
>message)
>{
> FILE* fp = fopen(path, "a");
> fprintf(fp, "\n%s", static_cast<const char*>(message));
> fclose(fp);
>}
>
>I would prefer to write the function like this:
>
>//Example B:
>
>void Test::WriteLog(const char* path, const char* message)
>{
> FILE* fp = fopen(path, "a");
> fprintf(fp, "\n%s", message);
> fclose(fp);
>}
>
>This uses the CString LPCTSTR operator (e.g., const char*)
>and has the advantages over example A. When you call
>Example A WriteLog with a literal string, e.g.,
>
>WriteLog("c:\\temp\\temp.log", "Hello");
>
>a CString message object has to be created (memory
>allocated). With Example B no object is created, as the
>compiler just passes a pointer to the literal. This is
>more efficient, particularly if you are doing a lot of
>logging.
>
>When you call Example A with a CString object, e.g.,
>
>CString sTemp("Hello");
>WriteLog("c:\\temp\\temp.log", sHello);
>
>because message is a reference, only a pointer is passed.
>With Example B, the CString operator LPCTSTR returns the
>internal CString pointer to the string data. Either way
is
>equally as efficient.
>
>For this reason I avoid CString& for function parameters.
>
>After all that, in MFC you should be using CStdioFile.
>
>
>
>>-----Original Message-----
>>Hello,
>>
>>I've a problem writing a CString to file.
>>I first convert to char* but it does not seem to work.
>>The code is below ...
>>
>>Bob
>>
>>
>>void Test::WriteLog(char* path,CString message)
>>{
>>char* text = new char[1024];
>>text = (char*)message.GetBuffer(sizeof(text));
>>FILE* fp = fopen(path,"a");
>>fprintf(fp,"\n%c",text);
>>fclose(fp);
>>delete text;
>>}


Re: write CString to file by Graeme

Graeme
Thu Jul 31 01:55:14 CDT 2003

Adrian wrote:
>
> OK, I see this message board is treating the static_cast
> syntax as though it contains an HTML tag, which it is
> stripping out, therefore when I post
>
> static_cast lessthan const char* greaterthan (message)
>
> (replace lessthan and greaterthan with the appropriate
> symbols) it is appearing as
>
> static_cast(message)
>
> Doh!
>
The newsgroup is fine - I suggest you check the software you're using to
view it :-)