How to buffer output?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 23
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster

How to buffer output?

 
0
  #1
Oct 28th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: ankur_ is an unknown quantity at this point 
Solved Threads: 2
ankur_ ankur_ is offline Offline
Light Poster
 
0
  #2
Oct 29th, 2009
Originally Posted by raigs View 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. }

What is SIZEOFBUFFER and BUFFER in your code and can you explain clearly what you want
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #3
Oct 29th, 2009
Originally Posted by raigs View 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. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster
 
0
  #4
Oct 29th, 2009
Originally Posted by dkalita View Post
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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 318 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC