LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2008, 08:39 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
vsprintf and va_list - string length?


Ok, so how to explain this easy?

Well I have a function that takes a char* and a va_list as arguments.
It will then send the complete, processed string to another function.

Example:
Code:
void function(char *txt, va_list args)
{
     /* the char way
      * problem: don't know the length of txt + arguments
      */
     char *string1 = malloc(???);
     vsprintf(string1, txt, args);
     another_function(string1);

     /* the string way
      * problem: don't know how to use it with vsprintf()
      */
     string string2;
     ???
     another_function(string2.c_str());
}

void another_function(char *txt)
{
    printf("I got: %s\n", txt);
}
So, two ways to go:

1) Use char* with malloc. I need to know how long the complete string will be to avoid having to chop it.

2) Use string. But can I use it with a va_list?
 
Old 02-21-2008, 09:19 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

I don't think there is a standard C/C++ way of handling this. But GNU has some functions that are not standard, but avaiable in Linux: vasprintf and asprintf. They work like sprintf, but you don't pass a string pointer, but the address of one. The string is then allocated for you, so you don't need to worry about size.

Example:

Code:
#define GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

void myprintf(const char *format, ...)
{
  char *ptr;
  va_list list;
  va_start(list,format);
  vasprintf(&ptr,format,list);
  printf("Formated: %s\nLength=%d\n", ptr, strlen(ptr));
  free(ptr);
  va_end(list);
}

int main(int argc, const char *argv[])
{
  myprintf("Hello %s", "world");
  return 0;
}
Too bad those functions are not in the C/C++ standard...
 
Old 02-21-2008, 10:09 AM   #3
cicorino
Member
 
Registered: May 2006
Location: Italy
Distribution: Slackware, Slackware64
Posts: 31

Rep: Reputation: 16
Does the standard vsnprintf with a small size argument (1 for example) return the size minus one required by your malloc? If so it could avoid you to use nonstandard functions, but it could reduce a lot the speed of your code too because of a double call to vsnprintf.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get string length in C ++ for a c string without trailing \0 nc3b Programming 10 12-28-2007 09:46 AM
Bash script to check the input string length fjkum Programming 3 06-30-2007 08:43 PM
string length ramesh_manu Linux - Newbie 1 02-24-2007 12:33 PM
Limit a string length for translation? mic Programming 3 01-20-2006 06:58 PM
is there any limit for string length in basic_string? pippet Programming 13 02-01-2005 06:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration