Question

I'm currently the Teaching Assistant for an Introduction to C class. The class is being taught using Visual Studio, but when grading I just use a simple Windows batch script to process all the assignment submissions, compile them, run them on a test file, and redirect the output to a series of text files I can print out, mark up, and hand back to students. The whole process works very well, except for the fact that when I redirect stdin, it does not appear in the redirected stdout the same way it does when the same stdin is typed directly into the console. Because of this, the output of code formatted for the console does not display correctly in the redirected output. The following file snippets show this problem. Does anyone know of a simple solution?

File: example.c

#include <stdio.h>

int main()
{
    int v;
    printf("Enter a number: ");
    scanf("%i", &v);
    printf("You entered: %d\n", v);
    return 0;
}

File: input.txt

42

Output (Console)

C:\>example.exe
Enter a number: 42
You entered: 42

C:\>

Output (Redirection)

C:\>example.exe < input.txt > output.txt

C:\>more output.txt
Enter a number: You entered: 42

C:\>
Was it helpful?

Solution

This is expected (correct) behaviour. The input is never part of stdout. If you do example.exe > output.txt and blindly type in 42, you should expect that 42 also shows up only once in the output.

The only solution I could think of is that the terminal/shell records the session as a whole. Windows command shell is not capable of that. You could write your own terminal proxy though, which feeds stdin into the student's program and reads the output itself, while writing out both in a combined fashion. It is quite easy to fork for exection of another program and redirect that one's stdin/out under POSIX (provided to you by Cygwin), I don't know about plain DOS/Windows though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top