string c_str() data() cout printf text file containing '\0'

T

Tony Murphy

I've got an application that sends emails (not spam!). The application
reads a template file (html/text) into a string, the string is
processed and placeholders filled in as appropiate.

I call a 3rd party api for sending the email, the api is written in c,
so i need to convert the string into LPSTR (windows c style string?),
i'm from unix world and new to windows mutant.

I know that c_str() converts a string to a c string and data() writes
the characters of a string into an array and isn't necessarily null
terminated.

I use printf to see whats been sent to a user, printf("%s",x.c_str())
& printf("%s",x.data()), but both only print out part of the string

cout << " SENDING HTML EMAIL " << x << endl; works file and prints out
the entire string

the 3rd party application fortunately sends the email in full. but i
need to know why only part of the string is been seen by printf.

How can i check if a file contains a null terminating character? I've
tried printing out a hex dump of the string using the following
method, but it doesn't get to the end of the string either

void dumpHex(const string &stringToHex)
{
ostringstream os;
string::const_iterator i = stringToHex.begin();
string::const_iterator e = stringToHex.end();

while (i!=e) {
os << std::hex << static_cast<string::traits_type::int_type>(*i++)
<< " " << *i << " ";
}
os << ends;
printf("HEX DUMP: \n\n%s\n\n",os.str().c_str());
}

this problem is driving me mad. i've trawled this news group looking
for answers cause there nearly always something similar unless i'm
doing something very stupid and nobody else stupid enough has bothered
to post about something similar. i'm going round in circles.
 
K

Karl Heinz Buchegger

Tony said:
I've got an application that sends emails (not spam!). The application
reads a template file (html/text) into a string, the string is
processed and placeholders filled in as appropiate.

I call a 3rd party api for sending the email, the api is written in c,
so i need to convert the string into LPSTR (windows c style string?),
i'm from unix world and new to windows mutant.

I know that c_str() converts a string to a c string and data() writes
the characters of a string into an array and isn't necessarily null
terminated.

I use printf to see whats been sent to a user, printf("%s",x.c_str())
& printf("%s",x.data()), but both only print out part of the string

This means (and I think you have already recognized it), that there
is a '\0' character somewhere within.
cout << " SENDING HTML EMAIL " << x << endl; works file and prints out
the entire string

the 3rd party application fortunately sends the email in full. but i
need to know why only part of the string is been seen by printf.

How can i check if a file contains a null terminating character?

You mean the string you got by reading the file.

eg. by checking each byte.

ask the string about it's size :)

for ( i = 0; i < MyString.size(); ++i )
if( MyString == '\0' )
cout << "There is a 0 at position " << MyString << endl;
 
T

Tony Murphy

thanks karl, the file size initally was 9536, i got this using

file.seekg( 0, ios::end );
cout << "File size: " << file.tellg() << endl;
file.clear();
file.seekg(0, ios::beg);

the string size initally was 9315 (guess difference is something to do
with \n and \r\n?) but by the time i get to printing it having passed
it about abit is 9311.
This means (and I think you have already recognized it), that there
is a '\0' character somewhere within.

You mean the string you got by reading the file.

eg. by checking each byte.

ask the string about it's size :)

for ( i = 0; i < MyString.size(); ++i )
if( MyString == '\0' )
cout << "There is a 0 at position " << MyString << endl;


afriad it didn't show up any '\0' characters.

the bug from hell i think

thanks again
tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top