Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf to char string...

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
hey guys,
I have a printf statement that I want to transfer to a char string, so I can mail it out to the admin if there's a problem. How do I do the copy to the string?

Thanks
M
 
You haven't supplied much information but you could use sprintf() instead of printf(), this returns a string variable. Alternatively you could use fprintf to put the output into a file which could be used as an attachment. If you give a bit more information about how you currently use the printf I migt be able to elaborate.

Cheers - Gavin
 
ok, here's what I'm doing:

// call to subroutine //
LogIt(2,NOFLAGIT,"Exiting Program");
MailIt(mail_text);


// sub //
static int LogIt(level, flagit, f, p1,p2,p3,p4,p5,p6,p7,p8,p9)
char *f;
{
int printit;

if(log_level == 0)
printit = 0;
else if( (log_level < 0) && abs(log_level) == level)
printit = 1;
else if(level <= log_level)
printit = 1;
else printit = 0;

if(printit)
{
if(printf(&quot;%c%s - &quot;,flagit? '+':' ',fmt_time()) == -1)
goto err;

if(printf(f,p1,p2,p3,p4,p5,p6,p7,p8,p9) == -1 ||
putchar('\n') == EOF || fflush(stdout) == EOF)
{
err: (void) fprintf(stderr,&quot;%s: error %d - %s.\n&quot;,
process_name,errno, strerror(errno));

(void) fprintf(stderr,&quot;%s: Can't produce log messages.\n&quot;,
process_name);
(void) fprintf(stderr,&quot;%s: Terminating.&quot;, process_name);
exit(2);
}
}

return(0);
}

************************************

I need to get the printf to copy over to a string var so I can do this function:

static int MailIt(str)
char *str;
{
FILE *pipe;

if( pipe = popen(&quot;/usr/lib/sendmail -fmds1 mbarr@ray.com&quot;,&quot;w&quot;))
{
fputs(str,pipe);
(void) pclose(pipe);
}
}
 
also, the printf does go to the screen, too. I need it to run a log, plus if something goes wrong, send the last couple (if possible) LogIt calls in the email.
 
well where you have printf(&quot;%s %d&quot;,var, var); you could replace this with sprintf(target,&quot;%s %d&quot;,var, var) and the formatted string ends up in target.
 
what about this one:

if(printf(f,p1,p2,p3,p4,p5,p6,p7,p8,p9) == -1 || putchar('\n') == EOF || fflush(stdout) == EOF)


How would I do that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top