PDA

View Full Version : [SOLVED] c++ cout to a FILE* ~ how do I.



highspider
April 23rd, 2011, 08:59 PM
how to write to a FILE* with << or cout <<
all I really need is cout for popen().



FILE *mail;
mail = popen ("/usr/sbin/sendmail -t", "w");
fprintf (mail, "To: %s\n", Self_ID );
fprintf (mail, "From: %s\n", dataString.substr(Semailloc, Eemailloc - Semailloc) );
fprintf (mail, "Subject: %s\n", Subject);
fprintf (mail, "\n");
fprintf (mail, "%s", dataString.substr(Smsgloc, Emsgloc - Smsgloc) );
fprintf (mail, "\n.\n");


readlines don't work

james_mcl
April 23rd, 2011, 09:09 PM
Have you tried C++ style file handling?



std::string infile = "/usr/sbin/sendmail -t";
//I admit I'm not sure how the code's going to handle that -t in the
//filename.
//So this may require someone smarter than me.

std::ofstream fout(infile.c_str(), std::ios::out);
if (!fout)
{
//it didn't open the file, do some error-handling.
}
else
{
fout<<"To: "<<Self_ID<<"\n";
fout<<"From: "<<dataString.substr(Semailloc, Eemailloc - Semailloc)<<"\n";
fout<<"Subject: "<<Subject<<"\n";
fout<<"\n";
fout<<dataString.substr(Smsgloc, Emsgloc - Smsgloc);
fout<<"\n.\n";
}

james_mcl
April 23rd, 2011, 09:10 PM
I should really have called that "outfile", rather than "infile". Oh well...

ve4cib
April 23rd, 2011, 09:11 PM
A few questions if I might...

You can confirm that the black lines do work though?

What are the contents of the file/stream when you run your code?

If you change the FILE* to be a plain text file does it work then?


My first reaction is that the problem may be with your substr calls. If they're not returning anything -- or returning the wrong thing -- that could be causing errors in the output.

highspider
April 23rd, 2011, 09:15 PM
A few questions if I might...

You can confirm that the black lines do work though?

What are the contents of the file/stream when you run your code?

If you change the FILE* to be a plain text file does it work then?


My first reaction is that the problem may be with your substr calls. If they're not returning anything -- or returning the wrong thing -- that could be causing errors in the output.

yes the whole program does work with strings.
just not dataString.substr() with fprintf.

and this debug works 100% also.


cout << "<p>" << contentLength << "</p>"
<< "<p>dataString:" << dataString << "</p>"
<< "<br />Snameloc:" << Snameloc
<< "<br />Enameloc:" << Enameloc << "<br />"
<< dataString.substr(Snameloc, Enameloc - Snameloc)
<< "<br />Semailoc:" << Semailloc
<< "<br />Eemailoc:" << Eemailloc << "<br />"
<< dataString.substr(Semailloc, Eemailloc - Semailloc)
<< "<br />Sphoneloc:" << Sphoneloc
<< "<br />Ephoneloc:" << Ephoneloc << "<br />"
<< dataString.substr(Sphoneloc, Ephoneloc - Sphoneloc)
<< "<br />Smsgloc:" << Smsgloc
<< "<br />Emsgloc:" << Emsgloc << "<br />"
<< dataString.substr(Smsgloc, Emsgloc - Smsgloc) << endl;

ve4cib
April 23rd, 2011, 09:20 PM
The substr function returns a string object, not a char*. The "%s" placeholder in fprintf expects a pointer to a char with a null terminator somewhere, not a string object. That could be what's causing your problem.

highspider
April 23rd, 2011, 09:35 PM
solved with .c_str() thanks james_mcl (http://ubuntuforums.org/member.php?u=247099)


mail = popen (MAIL_PROGRAM, "w");
fprintf (mail, "To: %s\n", Self_ID );
fprintf (mail, "From: %s\n", dataString.substr(Snameloc, Enameloc - Snameloc).c_str() );
fprintf (mail, "Subject: %s\n", Subject);
fprintf (mail, "\n");
fprintf (mail, "Name [%s]\nEmail [%s]\nPhone [%s]\n\n%s",dataString.substr(Snameloc, Enameloc - Snameloc).c_str(),
dataString.substr(Semailloc, Eemailloc - Semailloc).c_str(),
dataString.substr(Sphoneloc, Ephoneloc - Sphoneloc).c_str(),
dataString.substr(Smsgloc, Emsgloc - Smsgloc).c_str() );
fprintf (mail, "\n.\n");
pclose (mail);