Search code examples
c++file-iodijkstra

howt to automate reading input sample test case in c++


I'm coding Dijkstra algorithm and want to take a lot of test cases and no manual input allowed , I have two main files map.txt and routes.txt , i wanna take numbers as pairs , as shown in photossample test cases


Solution

  • You can redirect the input to stdin as follows:

    freopen("filepath.txt", "permission", stdin);
    
    • filepath: It is usually the name of the given input file (usually, in contests, the name is problem-name.txt.

    • permission: It is usually r or w. r means read which is used with input and w means write which is used with output.

    • stdin is the standard input, where you could use this to redirect the file to standard input and use the normal cin, and stdout is the standard output which is used to redirect your output to a file. In your case, you could use

      freopen("Map.txt", "r", stdin); to read your input from a file and then use the normal cin command, and you could use

      freopen("Routes.txt", "w", stdout); to output your results in the needed file.