views:

4970

answers:

1

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:\>
+3  A: 

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.

ypnos
I realize that the behavior I'm seeing is correct. What I would like to do is fake the behavior of the console setup with a script. Do you have short example of how to do that with POSIX? (I've worked with Cygwin/*nix extensively before and have no issue with using either if that's what it takes.)
Joseph Sturtevant
Let me see if I find some example code later.
ypnos
Look here for a good example of forking w/ redirection and then exec:http://publicabstractvoid.blogspot.com/2008/01/forkexec-while-redirecting-io.html
ypnos
If you don't know how to create a file desc. which you can pass for redirection, try perhaps using a UNIX pipe.
ypnos