Question Authors' Forum

Redirect from file to stdin

Redirect from file to stdin

by Al Spay -
Number of replies: 5

Hello.

There was a need to submit a large number of numbers to the program (up to 10^9). Students can't use file input yet. How to redirect input from in the template "input.txt " on Test.stdin.

There may be several files with sets of numbers.

In reply to Al Spay

Re: Redirect from file to stdin

by Richard Lobb -
10^9 numbers is going to a text file of at least several gigabytes. This is going to cause problems.

  1. It will almost certainly exceed the server's php.ini settings for post_max_size and upload_max_filesize
  2. It will probably also exceed the Moodle system max filesize setting and possibly a per-course max filesize.
  3. The Jobe server will maintain a cached copy of the file but it will have to be copied into the working directory for every student run, which will take time.
  4. The time to read and process a datafile of that size will likely exceed both the maximum allowed Jobe server run time and the maximum allowed memory.
Do you really want to have to work around all of those problems, none of which are directly related to the CodeRunner question itself? (1) and (2) will require system administrator assistance and if you're using a University production server, permission is quite likely to be refused.

I suggest considering what the desired learning outcome is from the exercise and then whether smaller datasets might achieve the same outcome. Perhaps simply pasting a large input data set into the Input field(s) of the question's test case(s) will suffice?

If you still wish to proceed, please post back, telling us what language you're using and I'll see what I can come up with. 

In reply to Richard Lobb

Re: Redirect from file to stdin

by Al Spay -
My error: 10^6 (7-8 Mb).
In reply to Al Spay

Re: Redirect from file to stdin

by Richard Lobb -
OK, that might work. What language are the students writing their answers in?
In reply to Richard Lobb

Re: Redirect from file to stdin

by Al Spay -
In reply to Al Spay

Re: Redirect from file to stdin

by Richard Lobb -
Firstly, are you sure you need to read the numbers from a file? I just created a Python3 CodeRunner question where the answer is a program to read numbers from stdin until EOF and then to print the count of numbers read and their average. I generated a test file of 10^6 random floats, one per line, and pasted the entire 8 MB file into the Test Case Standard Input field. It took several seconds to process the paste and then to save the question, but it ran just fine. All one million numbers were read and processed. The entire stdin contents aren't displayed in the result table, as CodeRunner snips out the middle of very long strings anywhere in the result table. If you wish to hide the standard input column altogether you can do so by turning on Customise and editing the Result Column field.

If you do wish to redirect stdin to a text file supplied as a support file, it's easy in Python, harder in C++.

In Python you can customise the question, turn off the Is Combinator checkbox and replace the template with a much simpler version (because it runs a job for each test case separately) that inserts before the student answer a couple of lines of code to redirect stdin as shown in the following image. The name of the datafile for the testcase must be inserted in the testcase Extra field.

The customised template and Combinator checkbox setting.

This approach allows every test case for the question to read from a different file.

There is no such easy option in C++ however. You have two options:
  1. If you can get by with just a single testcase for the question you can customise it and set up the Sandbox parameters field to include a runargs parameter (denoting "run arguments") like the following (where nums.txt has been added to the question as a support file):

    Showing runargs
  2. If you want something like the Python solution, with a different file for each test case, you will need to take complete control of the compile-and-execute process by scripting it in Python as explained in the documentation here. That example shows the general approach for C; you would need to change it for C++ and include an extra parameter in the subprocess.check_output call to set up stdin to the file nominated by the Extra field of the testcase. I doubt it's worth the effort but postback if you need more details.