-1

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

  • reading the first line would be something like std::ifile map{"map.txt"}; int value1, value2; ifile >> value1 >> value2; More on basic file I/O here : https://www.learncpp.com/cpp-tutorial/basic-file-io/ – Pepijn Kramer Jan 03 '22 at 11:41

1 Answers1

0

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.

SadekMo
  • 20
  • 3
  • This will work, but it is also more the "C" style of file handling. streams allow for code that's more reusable (e.g. write to a socket, std::io or a file). https://stackoverflow.com/questions/6399822/reading-a-text-file-fopen-vs-ifstream – Pepijn Kramer Jan 03 '22 at 11:43