//将一个文本文件中的字符颠倒顺序。
#include"../实践/std_lib_facilities.h"
int main()
try
{
//以默认模式打开文件。
cout << "Please enter the file name:";
string file_name;
cin >> file_name;
ifstream ifs(file_name.c_str());
if (!ifs)error("can't open ", file_name);
//--------------------------------------------------------------
//将文件内容读入向量。
vector<string>lines;
string line;
while (getline(ifs, line))lines.push_back(line);
//---------------------------------------------------------------
//以覆盖写入模式打开文件。
ofstream ofs(file_name.c_str(), ios_base::out);
if (!ofs)error("can't open ", file_name);
//---------------------------------------------------------------
//将向量内容倒叙写入文件中。
for(int i=lines.size()-1;i>=0;i--)
for (int j = lines[i].size() - 1; j >= 0; j--)
{
ofs << lines[i][j];
if (j == 0)ofs << '\n';
}
//----------------------------------------------------------------
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
};
//--------------------------------------------------------------------
将一个文本文件中的字符颠倒顺序。
于 2023-04-30 09:48:29 首次发布