// Illustrates output formatting instructions. // Reads all the numbers in the file rawdata.dat and writes the numbers // to the screen and to the file neat.dat in a neatly formatted way. // // C:\cs2>type rawdata.dat // 1.5 -21 // 4 8 -8.865 -91.91919191 // // C:\cs2>format // +1.50000 // -21.00000 // +4.00000 // +8.00000 // -8.86500 // -91.91919 // #include #include #include #include void make_neat(ifstream&, ofstream&, int, int); void main() { ifstream fin; ofstream fout; fin.open("rawdata.dat"); if (fin.fail()) { cout << "Input file opening failed.\n"; exit(1); } fout.open("neat.dat"); if (fout.fail()) { cout << "Output file opening failed.\n"; exit(1); } make_neat(fin, fout, 5, 12); fin.close(); fout.close(); } //Uses iostream.h, fstream.h, and iomanip.h. void make_neat(ifstream& messy_file, ofstream& neat_file, int number_after_decimalpoint, int field_width) { neat_file.setf(ios::fixed); neat_file.setf(ios::showpoint); neat_file.setf(ios::showpos); neat_file.precision(number_after_decimalpoint); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.setf(ios::showpos); cout.precision(number_after_decimalpoint); double next; while (messy_file >> next) { cout << setw(field_width) << next << endl; neat_file << setw(field_width) << next << endl; } }