I am trying to read from a file which has the content
Mohan
Singh
And write it into another file. The code I am using is the following
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create a text string, which is used to output the text file
string myText;
// Read from the text file
ifstream MyReadFile("demo.txt");
FILE *f1;
f1 = fopen("demo_out.txt","w+");
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
fprintf(f1, "%s\n", myText);
}
fclose(f1);
// Close the file
MyReadFile.close();
}
But it is storing some gibberish output in the file. Where am I going wrong?