1

I'm using sublime text 3 for competitive programming. I prefer C++ language. As I have to run my program again and again to check the output, it is easier to write down the input to a file and read input from there. For this I always write some extra lines on my program to read from a file and I comment out those lines before submitting my solution. Can you suggest any way to get rid of this? I am hoping that there is a way to change the default input source by which my program will take input from a file rather than standard input.

froghramar
  • 317
  • 2
  • 15

1 Answers1

1

You can write conditional code with freopen (works with C++ as well). One way supported by CodeChef is

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

. . . 

cin >> something;   // This will be read from 'in.txt' if ONLINE_JUDGE is not defined

The macro ONLINE_JUDGE can differ, but freopen is a way to change the input stream source.

EDIT: To achieve this in sublime text 3, write a custom build system (Sublime text 3 - compile program and run in terminal) and use I/O redirector from command line, for instance

g++ -Wall -o code code.cpp
./code < in.txt

This will read input from the file as if it were stdin.

Hope it helps.

Community
  • 1
  • 1
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50