473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
2 9173


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
5
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
10
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called...
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
1
by: lumo2000 | last post by:
hello NG, i am trying to do some syscalls and therefore i need to put some text together. its no problem as long i want to cout the text to display, but when i want to use it as parameter for...
2
by: hogcia | last post by:
Hello! I've got a little problem - I'm writing a program in C++, which should compare a text input from keyboard with a regular expression and return what parttern was recognized and where. The...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.