jeudi 28 janvier 2021

Windows executable not printing to standard output when standard input is redirected from a file

I have a C++ program that I wish to test with certain test cases. I am doing the testing locally. Normally, what I do is create a single test manually, and input it as the standard input for the program, and it prints the output on standard output.

Though the exact code is not very useful here, you can find it here (It's borderline unreadable, please do ask me directly for any queries). The main point being, standard input and output are being used.

So in order to create some test cases and run them on this program, I decided to create another program that creates test cases. Here is it's code:

#include<bits/stdc++.h>
using namespace std ;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout<<64<<"\n" ;
    for(int i=0;i<64;i++)
    {
        cout<<"2 3\n" ;
        int a = (i&1)>0 ;
        int b = (i&2)>0 ;
        int c = (i&4)>0 ;
        int d = (i&8)>0 ;
        int e = (i&16)>0 ;
        int f = (i&32)>0 ;
        cout<<a<<b<<c<<"\n";
        cout<<d<<e<<f<<"\n" ;
    }
}

Now let's say the main program has the name BinaryTable.cpp and this test program has the name BinaryTestGen.cpp. So what I do now is :

  1. Compile the main program : g++ -Wall -Wextra -g BinaryTable.cpp -o BinaryTable
  2. Compile the test program : g++ -Wall -Wextra -g BinaryTableGen.cpp -o BinaryTableGen
  3. Now the executables BinaryTable.exe and BinaryTableGen.exe are created (I am using mingw)
  4. Now I redirect the output of the test program to a file BinaryTableTxt.txt by BinaryTableGen.exe > BinaryTableTxt.txt
  5. This creates a 3 KB file BinaryTableTxt.txt
  6. Redirect this file to our main program : BinaryTable.exe < BinaryTableTxt.txt

The output is blank space. Nothing.

Now I run BinaryTable.exe directly, and copy-paste the contents of BinaryTable.txt directly to the cmd, then output is generated.

What am I doing wrong here?

I want the output to be printed to the console even when I am redirecting the input from a file.

Aucun commentaire:

Enregistrer un commentaire