0

I have a variable list function

/* vsprintf example */
 #include <stdio.h>
 #include <stdarg.h>

 void PrintFError (char * format, ...)
 {
    char buffer[50];
    va_list args;
    va_start (args, format);
    vsprintf (buffer,format, args);
    perror (buffer);
    va_end (args);
 }

 int main ()
 {
     FILE * pFile;
     char szFileName[]="myfile.txt";
     int firstchar = (int) '#';

     pFile = fopen (szFileName,"r");
     if (pFile == NULL)
        PrintFError ("Error opening '%s'",szFileName);
     else
     {
        // file successfully open
        fclose (pFile);
     }
     return 0;
 }

In above example how can we check the received message in "PrintError" function that we are not crossing the buffer size in this example 50 while using "vsprintf" in above example. This should be achieved in portable way.

7
  • Or vfprintf followed by perror( NULL ) Commented Jan 13, 2011 at 15:09
  • If you need to just print the message (and not save it in some variables) use vprintf (or vfprintf if you want to print on standard error or other files) instead of vsprintf: it will directly print the error, instead of saving it in a variable. Commented Jan 13, 2011 at 15:09
  • And note that if vsprintf sets errno, then your error message will be inappropriate. Commented Jan 13, 2011 at 15:10
  • Why put the C++ tag on this question? C and C++ are completely different languages. Commented Jan 13, 2011 at 15:11
  • @Binary Worrier: Because such functions are valid in C++, although they're much less necessary and generally discouraged. Commented Jan 13, 2011 at 15:14

4 Answers 4

4

You should use the safer vsnprintf, and limit it to 50 characters maximum.

int vsnprintf(char *str, size_t size, const char *format, va_list args);
Sign up to request clarification or add additional context in comments.

Comments

1

You are correct to worry about buffer overflow. You can't do this with vsprintf, but you can with vsnprintf, which includes an argument which is the length of the buffer.

Comments

0

Use vsnprintf(). It allows you to specify the number of characters to output (n):

int vsnprintf(char *s, size_t n, const char *format, va_list ap);

Comments

0

You can use vsnprintf. This is, strictly speaking, non-standard unless you have a C99 compiler, but is supported on most environments. If you do not have an implementation of vsnprintf on your platform, you can simply add a portable implementation to your program.

3 Comments

is it posisble to do in portable way
C99 is a ratified standard, so vsnprintf is, strictly speaking, standard.
@Steve, I suppose, but the kinds of compilers that, in C89, would not have implemented vsnprintf, probably haven't gotten around to implementing C99 today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.