//流作为函数实参 //格式化输出 #include<iostream> #include<iomanip> #include<cstdlib> #include<fstream> using namespace std; void make_neat(ifstream messy_file, ofstream neat_file, int num_a_decimalpoint, int field_width); int main() { ifstream fin; ofstream fout; fin.open("rawdata.dat"); if(fin.fail()) { cout<<"输入文件打开失败。/n"; exit(1); } fout.open("neat.dat"); if(fout.fail()) { cout<<"输出文件打开失败。/n"; exit(1); } make_neat(fin, fout, 5, 12); fin.close(); fout.close(); cout<<"程序结束。/n"; return 0; } void make_neat(ifstream messy_file, ofstream neat_file, int num_a_decimalpoint, int field_width) { neat_file.setf(ios::fixed); neat_file.setf(ios::showpoint); neat_file.setf(ios::showpos); neat_file.precision(num_a_decimalpoint); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.setf(ios::showpos); cout.precision(num_a_decimalpoint); double next; while(messy_file >> next) { cout<<setw(field_width)<<next<<endl; neat_file<<setw(field_width)<<next<<endl; } } VC++6.0下编译通过。 但运行时,程序结束后会报内存错误,不知为何。