944,202 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1003
  • C RSS
Oct 28th, 2009
0

How to buffer output?

Expand Post »
How to:
1. Buffer output ?
2. Get its size ?
3. Print the buffer ?
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. printf("Tons of printf lines\n");
  6.  
  7. // 1. Somehow buffer the output up until this point
  8.  
  9. printf("The size of the buffer is: %i\n", SIZEOFBUFFER); // 2. Get and print the size of the buffer.
  10.  
  11. printf("Here is the output:\n\n%s", BUFFER); // 3. Print the buffer.
  12.  
  13. return 0;
  14. }
Reputation Points: 9
Solved Threads: 0
Light Poster
raigs is offline Offline
25 posts
since Oct 2009
Oct 29th, 2009
0
Re: How to buffer output?
Click to Expand / Collapse  Quote originally posted by raigs ...
How to:
1. Buffer output ?
2. Get its size ?
3. Print the buffer ?
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. printf("Tons of printf lines\n");
  6.  
  7. // 1. Somehow buffer the output up until this point
  8.  
  9. printf("The size of the buffer is: %i\n", SIZEOFBUFFER); // 2. Get and print the size of the buffer.
  10.  
  11. printf("Here is the output:\n\n%s", BUFFER); // 3. Print the buffer.
  12.  
  13. return 0;
  14. }

What is SIZEOFBUFFER and BUFFER in your code and can you explain clearly what you want
Reputation Points: 34
Solved Threads: 2
Light Poster
ankur_ is offline Offline
38 posts
since Oct 2009
Oct 29th, 2009
0
Re: How to buffer output?
Click to Expand / Collapse  Quote originally posted by raigs ...
How to:
1. Buffer output ?
2. Get its size ?
3. Print the buffer ?
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. printf("Tons of printf lines\n");
  6.  
  7. // 1. Somehow buffer the output up until this point
  8.  
  9. printf("The size of the buffer is: %i\n", SIZEOFBUFFER); // 2. Get and print the size of the buffer.
  10.  
  11. printf("Here is the output:\n\n%s", BUFFER); // 3. Print the buffer.
  12.  
  13. return 0;
  14. }
may be something like
  1. #define MAX_BUFFER 1000
  2. char BUFFER[MAX_BUFFER];
  3. BUFFER[0] = '\0';
  4. strcat(BUFFER, toPrint);/*toPrint contains whatever u wanted to print*/
  5. /*but check for the buffer content size because if it exceed MAX_BUFFER u wil get seg fault*/
  6. /*do it for every printf("%s", toPrint)*/
  7. printf("The size of the buffer is: %i\n", strlen(BUFFER)); // 2. Get and print the size of the buffer.
  8. printf("Here is the output:\n\n%s", BUFFER); // 3. Print the buffer.
Last edited by dkalita; Oct 29th, 2009 at 4:06 am.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 29th, 2009
0
Re: How to buffer output?
Click to Expand / Collapse  Quote originally posted by dkalita ...
may be something like
  1. #define MAX_BUFFER 1000
  2. char BUFFER[MAX_BUFFER];
  3. BUFFER[0] = '\0';
  4. strcat(BUFFER, toPrint);/*toPrint contains whatever u wanted to print*/
  5. /*but check for the buffer content size because if it exceed MAX_BUFFER u wil get seg fault*/
  6. /*do it for every printf("%s", toPrint)*/
  7. printf("The size of the buffer is: %i\n", strlen(BUFFER)); // 2. Get and print the size of the buffer.
  8. printf("Here is the output:\n\n%s", BUFFER); // 3. Print the buffer.
Yes, that works. I thought there might be a cleaner way of doing it though. Maybe some keyword that I could use...
Reputation Points: 9
Solved Threads: 0
Light Poster
raigs is offline Offline
25 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem regarding trees in data structures using c
Next Thread in C Forum Timeline: Compile program for both 32 and 64 bit?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC