//读取一个文件,文件格式是以空白符间隔的数值,将这些数值输出到另一个文件,
//格式采用科学记数法,精度为8,每行4个域,每个域的宽度为20个字符。
#include"../实践/std_lib_facilities.h"
int main()
try
{
//打开输入文件。
cout << "Please enter the name of the file you want to read: ";
string read_file_name;
cin >> read_file_name;
ifstream ifs(read_file_name.c_str());
if (!ifs)error("can't open ", read_file_name);
//-------------------------------------------------------------------------
//打开输出文件。
cout << '\n' << "Please enter the name of the file you want to write: ";
string write_file_name;
cin >> write_file_name;
ofstream ofs(write_file_name.c_str());
if (!ofs)error("can't open ", write_file_name);
//--------------------------------------------------------------------------
//将输入文件内容读进向量中。
vector<double>figures;
double figure;
while (ifs >> figure)figures.push_back(figure);
//-------------------------------------------------------------------------
//将向量中的元素按照规定格式写入到输出文件中。
for (int i = 0; i < figures.size(); i++)
{
ofs << scientific << setprecision(8) << setw(20) << figures[i] << '|';
if ((i + 1) % 4 == 0)ofs << '\n';
}
//--------------------------------------------------------------------------
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
};
//--------------------------------------------------------------------
读取一个文件,文件格式是以空白符间隔的数值,将这些数值输出到另一个文件,格式采用科学记数法,精度为8,每行4个域,每个域的宽度为20个字符。
于 2023-04-30 14:26:26 首次发布