Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get append mode in sprintf()

Tags:

c

string

printf

See this code:

int main() {
    char a[50];
    FILE *fp;

    fp = fopen("test1.txt", "w");

    sprintf(a,"jigar %d \n", 3);
    fprintf(fp,"jigar %d \n", 3);

    sprintf(a,"patel %d \n", 2);
    fprintf(fp,"patel %d \n", 2);
    printf("%s", a);
}

Here, using fprintf, I can write in file

jigar 3 
patel 2 

where same functionality I want where what ever I print that goes in one char buffer.

but using sprintf gives me on buffer

patel 2 

I have so many such print which I want to add in one char buffer and then I need to return it to application so how to get it in simplest and fastest way this?

like image 622
Jeegar Patel Avatar asked Apr 24 '12 09:04

Jeegar Patel


People also ask

How do I append a string in sprintf?

Append string to sprintf() returned value This is the function that writes the value. Hi there, You can use new_string = strcat(s1,s2); or your can use sprintf (targetstring,"%d some literal here", advalue);

Does sprintf append or overwrite?

The sscanf () function and the sprintf () function are like the two sides of a coin. You can now use the sprintf() function to reassemble the string. You can use the same char array stringa- its previous value gets overwritten.

Why should we use Snprintf () instead of sprintf ()?

Snprintf is safer to use because characters are not omitted and it is stored in the buffer for later usage. Both sprintf and snprintf store the string and produces the output as needed by user.

What is Snprintf in C?

The snprintf is a predefined library function of the stdio. h header file, which redirects the output of the standard printf() function to other buffers. The snprint() function is used to format the given strings into a series of characters or values in the buffer area.


2 Answers

sprintf() returns the number of characters printed.

Just use that number for the next write ...

int i;
char a[50];
char *ap = a;

for (i = 5; i < 15; i++) {
    ap += sprintf(ap, "%d ", i);
}

printf("%s\n", a); /* "5 6 7 8 9 10 11 12 13 14 " */

Make sure you don't cause any buffer overflows though.

like image 161
pmg Avatar answered Nov 05 '22 07:11

pmg


The idea is to give sprintf the pointer to the base address of your buffer + an offset. The offset gets bigger while you are appending.

By using strlen it is possible to get the lenght of the current 'string'.To avoid a buffer overflow, we could add extra logic in order to check if the current 'string' + 'the new string' is less than the size of the buffer.

size_t offset = strlen(text);
sprintf(&(text[offset]), "This is how You append using sprintf");

Example within a loop:

char text[255];
text[0] = '\0';
for(int i = 0 ; i < 10 ; i++) {
    size_t offset = strlen(text);
    sprintf(&(text[offset]), "%d,", i);
}

As from other stackoverflow users suggested, you could also use snprintf, which would give you also buffer overflow protection.

An append function could look like this:

void append(char *text, int maxSize, char *toAppend) {
  size_t offset = strlen(text);
  snprintf(&(text[offset]), maxSize, "%s", toAppend);
}  
like image 33
Alessandro Giusa Avatar answered Nov 05 '22 08:11

Alessandro Giusa