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

1
  • What does snprintf return ? That may prove useful when potentially hitting an addendum snprintf, specifically regarding an offset into the target buffer. Commented May 20, 2019 at 16:30

1 Answer 1

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);
    }    
}
Sign up to request clarification or add additional context in comments.

8 Comments

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
at this line numBytes += snprintf(buffer + numBytes, sizeof(buffps) -numBytes, "\n");
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?
@monk can you please update your question with new problem?
@kiranBiradar , your answer correctly answer the original question. I will debug mybest, if required I will raise another question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.