Search code examples
c++dev-c++

Displaying large output on terminal window


int a[10000];
for(int i=0;i<10000;i++)
{
   a[i]=i; cout<<a[i]<<endl;
}

Suppose this is the code and on the terminal screen I need all outputs (0-9999) But it only displays (9704-9999) at the end

I want to see all the numbers on the terminal window but it removes the upper part of the data. I guess I have to change some settings.


Solution

  • Increase the console buffering. Depending on which terminal you're using it'll be different. For example on Windows conhost.exe is the default console used by cmd and PowerShell. Just click the icon on the top left > Properties > Layout and set Screen Buffer Size to a large enough number

    Conhost

    But the better solution would be redirecting to file, because no one wants to read 10000 lines on the console, and there's no guarantee that the console will have a buffer of infinite length or length of more than 10000 lines. conhost for example only supports maximum 9999 lines so you'll miss at least the command you typed and the first output line. Besides that'll often remove other commands' output from the history which is undesirable

    Either do that from the command line with the redirection operator >

    yourapp.exe >output.txt
    

    or save to file directly from your code