By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Tony Murphy wrote:

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[i] == '\0' )
cout << "There is a 0 at position " << MyString[i] << endl;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

P: n/a
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.
How can i check if a file contains a null terminating character?
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[i] == '\0' )
cout << "There is a 0 at position " << MyString[i] << endl;


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

the bug from hell i think

thanks again
tony
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.