using perror with formatted string

  • Follow


Hello,

I want to use perror like I use fprintf(stderr, ...)

fprintf(stderr, "format, ... %s. \n", my_str);

perror syntax is not the same as perror
(it takes only one char * argument)

How can I do the above and encapsulate it in one function in the most simple 
way.
(something like :  my_perror("format .... %s", <argument list>)

(Or can I declare a function with va_list , and send the va_list somehow to 
fprintf function ?)

Thanks :) 


0
Reply no_spam_please4 (116) 6/8/2007 8:53:51 AM

I have ment :
1) How can I pass one va_list to another va_list function ?
2) Should I do the above, or there is simpler solution for that ?

Thanks :) 


0
Reply no_spam_please4 (116) 6/8/2007 9:05:41 AM


"Mr. X." <no_spam_please@nospam_please.com> schrieb im Newsbeitrag 
news:f4b5hn$hpu$1@news2.netvision.net.il...
> Hello,
>
> I want to use perror like I use fprintf(stderr, ...)
>
> fprintf(stderr, "format, ... %s. \n", my_str);
>
> perror syntax is not the same as perror
> (it takes only one char * argument)
>
> How can I do the above and encapsulate it in one function in the most 
> simple way.
> (something like :  my_perror("format .... %s", <argument list>)
>
> (Or can I declare a function with va_list , and send the va_list somehow 
> to fprintf function ?)

perror(message);

(usually?) boils down to do the equivalent of:

fprintf(stderr, "%s: %s\n", message, strerror(errno));

Bye, Jojo 


0
Reply nospam.jojo (1344) 6/8/2007 9:15:49 AM

"Mr. X." <no_spam_please@nospam_please.com> writes:

> Hello,
> 
> I want to use perror like I use fprintf(stderr, ...)
> 


#include <string.h>
#include <stdio.h>
#include <errno.h>


fprintf (stderr, "Error: %s\n", strerror(errno) );
0
Reply tmp (4) 6/8/2007 9:48:57 AM

On Fri, 8 Jun 2007 11:53:51 +0300 Mr. X. <no_spam_please@nospam_please.com> wrote:

| I want to use perror like I use fprintf(stderr, ...)
| 
| fprintf(stderr, "format, ... %s. \n", my_str);
| 
| perror syntax is not the same as perror
| (it takes only one char * argument)

I had a similar desire over a decade ago.  I don't use perror() any more.
I always use fprintf() or snprintf() depending on the situation, along
with strerror().

It would be nice if there was a special format item to insert that would
take its value directly from errno (not from an argument) and insert the
string at that location.


| How can I do the above and encapsulate it in one function in the most simple 
| way.
| (something like :  my_perror("format .... %s", <argument list>)
| 
| (Or can I declare a function with va_list , and send the va_list somehow to 
| fprintf function ?)

Something like this?

int my_perror(const char *f,va_list a)
{
    int e,r,s;
    e=errno;
    if((s=vfprintf(stderr,f,a))<0)return s;
    if((r=fprintf(stderr,": %s",strerror(e)))<0)return r;
    return r+s;
}

-- 
|---------------------------------------/----------------------------------|
| Phil Howard KA9WGN (ka9wgn.ham.org)  /  Do not send to the address below |
| first name lower case at ipal.net   /  spamtrap-2007-06-08-0701@ipal.net |
|------------------------------------/-------------------------------------|
0
Reply phil-news-nospam (644) 6/8/2007 12:32:17 PM

4 Replies
31 Views

(page loaded in 0.084 seconds)


Reply: