I am just starting out with c++, but I have a goal in mid. I have already programmed the logical portion of what I wish to acomplish in Java, but it doesn't work, because I can't set a usb to read only for the whole system in Java (if I can;t do this in c++ then tell me now). I have this code and it can read the file fine, but when I write to it and then open the file it doesn't actually write to the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream myFile ("C:/Test/C++FileRW/test.txt");
string line;
if (myFile.is_open()){
getline (myFile, line);
cout << line << "\n";
myFile << "\nExtra Text Added";
cout << "Wrote to file";
myFile.close();
}else {
cout << "Unable to open file";
}
cin.ignore(1);
return 0;
}
This should either append the Extra Text Added, or it should replace the original text, but it doesn't do either of those. What is the problem, and how can I fix it?
Thank you for your help.