C/Strncatf

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
This is a variation of <code>strcat()</code> with a few important differences / improvements:
+
=[http://ehiqikag.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
* allows you to pass a formatted string like <code>printf()</code>
+
This is a variation of &lt;code&gt;strcat()&lt;/code&gt; with a few important differences / improvements:
* takes a max length for dest, not for how much of src to use (like <code>strncat()</code>)
+
* allows you to pass a formatted string like &lt;code&gt;printf()&lt;/code&gt;
* returns a pointer to the <code>\0</code> at the end of dest after the operation.
+
* takes a max length for dest, not for how much of src to use (like &lt;code&gt;strncat()&lt;/code&gt;)
 +
* returns a pointer to the &lt;code&gt;\0&lt;/code&gt; at the end of dest after the operation.
  
<source lang="c">
+
&lt;source lang=&quot;c&quot;&gt;
 
char *strncatf(char *dest, int length, char *format, ...) {
 
char *strncatf(char *dest, int length, char *format, ...) {
 
   char buf[4096];
 
   char buf[4096];
Line 13: Line 14:
 
   length -= strlen(dest);
 
   length -= strlen(dest);
 
   strncat(dest,buf,length-1);
 
   strncat(dest,buf,length-1);
   return &dest[strlen(dest)];
+
   return &amp;dest[strlen(dest)];
 
}
 
}
</source>
+
&lt;/source&gt;

Revision as of 01:27, 24 November 2010

This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page

This is a variation of <code>strcat()</code> with a few important differences / improvements:

  • allows you to pass a formatted string like <code>printf()</code>
  • takes a max length for dest, not for how much of src to use (like <code>strncat()</code>)
  • returns a pointer to the <code>\0</code> at the end of dest after the operation.

<source lang="c"> char *strncatf(char *dest, int length, char *format, ...) {

 char buf[4096];
 va_list ap;
 va_start(ap,format);
 vsnprintf(buf,sizeof(buf),format,ap);
 va_end(ap);
 length -= strlen(dest);
 strncat(dest,buf,length-1);
 return &dest[strlen(dest)];

} </source>

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox