Example 1 -------------------------------------------------------------- #include #include using namespace std; int main() { // Create and open a text file ofstream MyFile("filename.txt"); // Write to the file MyFile << "Files can be tricky, but it is fun enough!"; // Close the file MyFile.close(); } Example 2 ------------------------------------------------------------ #include #include using namespace std; int main() { // Create a text string, which is used to output the text file string myText; char ch; // Read from the text file ifstream MyReadFile("filename.txt"); // Read by using getline() function to read the file line by line cout << "Read by getline()" << endl; while (getline (MyReadFile, myText)) { // Output the text from the file cout << myText << endl; } MyReadFile.close(); ifstream MyReadFile2("filename.txt"); cout << "Read character by character" << endl; while( MyReadFile2 >> ch){ // output the character cout << ch; } // Close the file MyReadFile2.close(); } Example 3 ------------------------------------------------------------ #include #include using namespace std; int main() { string filename; fstream myfile; cout << "Enter filename: "; cin >> filename; const char* fcs = filename.c_str(); myfile.open(fcs,ios::app); myfile << "This is a sentence" << endl; // Close the file myfile.close(); }