I am using my last week's assignment to read and write my reversed string to a file but I have no idea how to go about it, any ideas? I tried looking into fstream but it is a bit confusing. Here is what I got so far, a non-working code of course.
One mistake here is that you named your ifstream and ofstream the same variable name. This is just like if you had:
1 2
int a;
char a;
Not going to work so great.
For a beginner use of ifstream/ofstream, they can be viewed exactly as cout/cin. Expect that you have to declare, open, and close them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ifstream inFile("input.txt"); // opens input.txt for reading
ofstream outFile("output.txt"); // opens output.txt for writing
int i;
cin >> i; // Reads 'i' from your keyboard
inFile >> i; // Reads 'i' from input.txt
cout << i; // Write 'i' to the console
ouFile << i; // Write 'i' to output.txt
inFile.close();
outFile.close()
That's just to show the similarities between keyboard/files. Not really copy/paste into code material.
I don't want to get too complicated but the relationship is even closer. cin/isfstream are istreams, and cout/ofstream are ostreams. You can have something like this: