2、编写程序实现如下功能,将“hello C++”,“hello summer”这两句话写入文本data.txt,然后从文本中读取这两句话,在每个单词前后加上‘*’符号后,输出到屏幕,输出结果如下: *hello* *C++* *hello* *summer*
时间: 2023-06-14 21:07:47 浏览: 92
下面是C++的实现代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// 将两句话写入文本data.txt
ofstream outfile("data.txt");
outfile << "hello C++" << endl;
outfile << "hello summer" << endl;
outfile.close();
// 从文本中读取两句话并在每个单词前后加上'*'符号后输出到屏幕
ifstream infile("data.txt");
string word;
while (infile >> word) {
cout << "*" << word << "*" << " ";
}
infile.close();
return 0;
}
```
程序的运行结果为:
```
*hello* *C++* *hello* *summer*
```
阅读全文