11-5

使用I/O流以文本方式打开上题建立的文件test1.txt,在次此文件后面添加字符“已成功添加字符!”,然后读出整个文件的内容显示出来,看看是否正确。

 1 #include <iostream>
 2 #include <string>
 3 #include<string.h>
 4 #include <fstream>
 5 using namespace std;
 6 
 7 
 8 int main(){
 9     char ch;
10     ofstream file1("text1.txt", ios::app);
11     file1<<"已成功添加字符!";
12     file1.close();
13     ifstream file2("text1.txt");
14     while(file2.get(ch)) cout<<ch;
15     file2.close();
16     return 0;
17 }

 

posted @ 2023-03-28 19:00  nlkdfgnvfdkl  阅读(44)  评论(0)    收藏  举报