Results 1 to 7 of 7

Thread: c++ cout to a FILE* ~ how do I.

  1. #1
    Join Date
    Dec 2010
    Location
    Earth
    Beans
    151
    Distro
    Ubuntu 14.04 Trusty Tahr

    c++ cout to a FILE* ~ how do I.

    how to write to a FILE* with << or cout <<
    all I really need is cout for popen().

    Code:
    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
    In a perfect world my college professors would allow assignment as .odt openoffice.org files! And code as Eclipse projects.

  2. #2
    Join Date
    Mar 2007
    Beans
    103
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: c++ cout to a FILE* ~ how do I.

    Have you tried C++ style file handling?

    Code:
    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";
    }
    Last edited by james_mcl; April 23rd, 2011 at 09:09 PM. Reason: accidentally clicked on thumbs-down icon, had to get rid of it.

  3. #3
    Join Date
    Mar 2007
    Beans
    103
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: c++ cout to a FILE* ~ how do I.

    I should really have called that "outfile", rather than "infile". Oh well...

  4. #4
    Join Date
    Jun 2007
    Location
    Canada
    Beans
    370

    Re: c++ cout to a FILE* ~ how do I.

    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.
    GCS/O d+(-@) s: a-->? C(++) UL P+ L+++@ E@
    W++$ N++ !o K++ w(++) !O M(-) !V PS+(++)
    PE-() Y+ PGP++ t++(+++@)* 5++ X++@ R+++@
    tv+ b++(+++) DI++ D+ G+ e++>++++ h- r y?

  5. #5
    Join Date
    Dec 2010
    Location
    Earth
    Beans
    151
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: c++ cout to a FILE* ~ how do I.

    Quote Originally Posted by ve4cib View Post
    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.
    Code:
    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;
    In a perfect world my college professors would allow assignment as .odt openoffice.org files! And code as Eclipse projects.

  6. #6
    Join Date
    Jun 2007
    Location
    Canada
    Beans
    370

    Re: c++ cout to a FILE* ~ how do I.

    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.
    GCS/O d+(-@) s: a-->? C(++) UL P+ L+++@ E@
    W++$ N++ !o K++ w(++) !O M(-) !V PS+(++)
    PE-() Y+ PGP++ t++(+++@)* 5++ X++@ R+++@
    tv+ b++(+++) DI++ D+ G+ e++>++++ h- r y?

  7. #7
    Join Date
    Dec 2010
    Location
    Earth
    Beans
    151
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: c++ cout to a FILE* ~ how do I.

    solved with .c_str() thanks james_mcl
    Code:
     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);
    Last edited by highspider; April 24th, 2011 at 02:19 AM. Reason: solved
    In a perfect world my college professors would allow assignment as .odt openoffice.org files! And code as Eclipse projects.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •