【C++---07】实验报告 文件操作

    xiaoxiao2025-02-12  17

    内容提要

    创建一个文件输出流对象,通过构造函数指定磁盘文件名。通过文件输出流对象,写入若干行文字。关闭文件输出流对象,然后观察磁盘文件内容。.改用open函数创建文件输出流,并选择适当的文件操作方式。在原内容的后面追加若干行新内容。基本要求:能熟练运用文件各种操作编写程序测试并提交程序。 #include <iostream> #include <fstream> #include <string> using namespace std; int main(void) { string str; ofstream outFlie("../outText.txt"); ofstream filego("./here", ios::binary); cout << "请输入要写入outText中的文字:"; cin >> str; outFlie << str; cout << "成功将\"" << str << "\"写入到文件中!\n"; outFlie.close(); outFlie.open("../outText.txt", ios::app); cout << "请输入要追加的文字:"; cin >> str; outFlie << str; cout << "成功将\"" << str << "\"写入到文件中!\n"; outFlie.close(); system("pause"); return 0; }

     

    最新回复(0)