#include #include #include #include using namespace std; string stretch(string s, const int len) { while (s.length() < len) s += ' '; return s; } int digits(const int n) { if (n < 10) return 1; return digits(n / 10) + 1; } string spaces(int n) { string r; while (n > 0) { r += ' '; n -= 1; } return r; } int main() { ifstream fi; fi.open("/home/www/class/een118/a3010b.txt"); if (fi.fail()) { cerr << "can't open\n"; exit(1); } ofstream fo; fo.open("elements.txt"); if (fo.fail()) { cerr << "Can't create\n"; exit(1); } string longest; int atno = 0, longestlen = -1; while (true) { string sym, name; double atwt; fi >> sym >> name >> atwt; if (fi.fail()) break; atno += 1; if (name.length() > longestlen) { longestlen = name.length(); longest = name; } fo << spaces(3 - digits(atno)) << atno << " " << stretch(name, 13) << " " << stretch(sym, 2) << " " << atwt << "\n"; } fi.close(); fo.close(); }