Rewrite the following program correcting any mistakes and adding missing code so that it reads a value from the file "input.txt" and writes it to the file "results.txt"
1 2 3 4 5 6 7 8 9 10 11 12
#include <bits/stdc++.h>
int main()
{
int n;
ifstream inData;
outData.open("results.txt)
cin >> n;
outData << n << endl;
return 0;
}
#include <fstream>
int main()
{
std::ifstream inData("input.txt");
if (inData) {
std::ofstream outData("results.txt");
if (outData) {
int n {};
if (inData >> n)
outData << n << '\n';
}
}
}
how to do it so next time it shows up on my test I can do it correctly
The issue is not that you will be able to do the same the next time you get asked, it's that you understand the principles as to how it's done so that you know/understand how to open files for reading/writing, how to read/write data to files and how to check if a file operation has completed ok. Once you understand this, then you'll be able to correctly do any file based question.