运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,暗/亮
前往 Spaces
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { // Create a text file ofstream MyWriteFile("filename.txt"); // Write to the file MyWriteFile << "Files can be tricky, but it is fun enough!"; // Close the file MyWriteFile.close(); // Create a text string, which is used to output the text file string myText; // Read from the text file ifstream MyReadFile("filename.txt"); // Use a while loop together with the getline() function to read the file line by line while (getline (MyReadFile, myText)) { // Output the text from the file cout << myText; } // Close the file MyReadFile.close(); }
文件可能很棘手,但它很有趣!