0

How to store the output of multiple commands OR block of code into a single variable. I need to write this into a variable so that it can be used in multiple places.

Following is a dummy program containing a block of code(see comment) which has two printf statements, I need to store them into a single variable so that it can be used later.

Sample code to show the output format

#include <stdio.h>

int main()
{

int c;

 // code block start here

char buffer[128];

for (c = 1; c <= 10; c++)
  {
     printf("%d",c+2);

     if(c%2){
             printf("%d\n",c+2);

        }

  }

//code block ends here


// store the whole output of above code block into variable

//send the data on socket  ---this is working ,but need the whole data into the variable
  return 0;
}

Above program result like this

 -->./a.out
33
455
677
899
101111
12   

I tried to use snprintf to store the output of the two printf into a variable called buffer but its overwriting the data of last printf.

#include <stdio.h>


int main()
{

int c;

 // code block start here

char buffer[128];

for (c = 1; c <= 10; c++)
  {
//      printf("%d",c+2);
                snprintf(buffer, sizeof(buffer), "%d", c+2);
      if(c%2){
//                printf("%d\n",c+2);
        snprintf(buffer, sizeof(buffer), "%d", c+2);
                }

  }

printf("buffer is %s\n",buffer);
//code block ends here


// store the whole output of above code block into variable

//send the data on socket  ---this is working ,but need the whole data into the variable
  return 0;
}

Current output:

buffer is 12

Desired output:

buffer is 33\n455\n677\n899\n101111\n12

monk
  • 1,953
  • 3
  • 21
  • 41
  • What does [`snprintf`](https://en.cppreference.com/w/c/io/fprintf) return ? That may prove useful when potentially hitting an addendum `snprintf`, specifically regarding an *offset* into the target buffer. – WhozCraig May 20 '19 at 16:30

1 Answers1

3

As of now you are overwriting the buffer everytime with latest snprintf call.

You need to consider last number of bytes written which snprintf returns.

Example:

int numBytes = 0;

for (c = 1; c <= 10; c++)
{
    numBytes += snprintf(buffer+numBytes, sizeof(buffer)-numBytes, "%d", c+2);
    if (c%2) {
        numBytes += snprintf(buffer+numBytes, sizeof(buffer)-numBets, "%d", c+2);
    }    
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Thanks , this is working with the dummy code, but when I integrated with the actual code I am getting `Bus error` like output from strace `write(1, "\n", 1 ) = 1 --- SIGBUS {si_signo=SIGBUS, si_code=SI_KERNEL, si_addr=0} --- +++ killed by SIGBUS +++ Bus error` – monk May 20 '19 at 16:39
  • at this line `numBytes += snprintf(buffer + numBytes, sizeof(buffps) -numBytes, "\n");` – monk May 20 '19 at 16:40
  • 1
    what is `buffps` in relation to `buffer`? More importantly, how big is your `buffer` and what is the value of `numBytes` at this point. With how simple that line of code is, the only explanation I can see is that `buffer + numBytes` does not point to a writeable area of memory. Could you be overrunning your buffer, and `snprintf` allowing it because `sizeof(buffps)` and `sizeof(buffer)` are different? – CryptoFool May 20 '19 at 16:55
  • I am sorry, I failed to update the output. `bufferps` represent `buffer` in the actual code. I did a `printf("numBytes = %d\n");` before this statement and got `00numBytes = 0` – monk May 20 '19 at 17:01
  • 1
    @monk can you please update your question with new problem? – kiran Biradar May 20 '19 at 17:11
  • 1
    @kiranBiradar , your answer correctly answer the original question. I will debug mybest, if required I will raise another question. – monk May 20 '19 at 18:34
  • Note that [`snprintf()`](https://port70.net/~nsz/c/c11/n1570.html#7.21.6.5) returns the number of bytes that would have been written, apart from the null byte, had the target string been big enough, or a negative number on a conversion error. Even if all goes well, this return value can be bigger than the second argument to the function; things are worse if there's a failure. You have to do more work to keep things straight on the book-keeping. It's fiddly. – Jonathan Leffler May 21 '19 at 18:12
  • If `sizeof(buffer)-numBytes` happens to fail and go negative, it gets recast into an unsigned near SIZE_MAX, and will happily keep writing outside the buffer. – Dave X Feb 10 '22 at 15:10