Click here to Skip to main content
15,889,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In order to spped the console output in windows it is recommended to use setvbuf().
My problem is that after making the buffer, cout does not work in called functions.

I had to insert at the following program at main() the line:
cout<<"=== FIN ==="<<endl;


So:
1. How force to write at cout within the function?
2. What happens if writting size is bigger than the buffer size?

What I have tried:

C++
<pre>#include <iostream>
using namespace std;
void test1(){cout<<" function ini"<<endl;} //" function ini" is not written here

int main()
{
#ifndef __linux__   //Introduce this code at the beginning of main() to increase a lot the speed of cout in windows: 
	char buffer_setvbuf[1024];setvbuf(stdout, buffer_setvbuf, _IOFBF, sizeof buffer_setvbuf); 
#endif

	cout<<"=== INI ==="<<endl;
	test1();
	cout<<"=== FIN ==="<<endl;
}


Note: I tried also set buffer_setvbuf[] as a global variable with same results.
Posted
Updated 7-Nov-17 1:23am
v3
Comments
Richard MacCutchan 7-Nov-17 7:13am    
Why do you need it, what problem are you trying to solve?
Javier Luis Lopez 7-Nov-17 8:56am    
Because printf and cout are very slow in windows, as can be seen here: https://stackoverflow.com/questions/11558540/c-why-is-a-fprintfstdout-so-slow as said CPallini

output is not displayed until the length of the buffer is reached (or output is flushed), see the answer here: C: Why is a fprintf(stdout,....) so slow? - Stack Overflow[^].

Quote:
1. How force to write at cout within the function?

I would maintain the line buffer policy. On the other hand, if you insist on using the full buffer one, then you have to explicitely flush cout.


Quote:
2. What happens if writting size is bigger than the buffer size?
The wonderful: on reaching buffer size, the buffer content is sent to the screen and the whole buffer is then available for further output.
 
Share this answer
 
Comments
Javier Luis Lopez 7-Nov-17 9:01am    
I tested point 2.
How is better to flush the cout?
1. By make cout<<endl; //not worked for me always
2. cout<<"Dataout=..."<<flush;
3. Making fflush(stdout); just before ending the function
CPallini 7-Nov-17 9:21am    
Of course point 1 shouldn't work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900