File and formatting examples for class use // I/O manipulators used for formatting output #include #include using std::cout; using std::fixed; using std::left; using std::right; using std::setprecision; using std::setw; int main(int argc, char* argv[]) { const char* names[] = { "Theodore", "Melinda", "Sasquatch", "Sue" }; double amts[] = { 37.5, 185.135, .2743, 409 }; int ctr = 0; double sum = 0.0; cout << "Name Amount\n"; cout << "----------- ------\n"; for (ctr=0; ctr<4; ctr++) { cout << setw(10) << left << names[ctr] << " " << setw(7) << fixed << right << setprecision(2) << amts[ctr] << '\n'; sum += amts[ctr]; } cout << "----------- ------\n"; cout << "Total " << setw(7) << fixed << right << setprecision(2) << sum << '\n'; return 0; } // printf used for formatting output #include int main(int argc, char* argv[]) { const char* names[] = { "Theodore", "Melinda", "Sasquatch", "Sue" }; double amts[] = { 37.5, 185.135, .2743, 409 }; int ctr = 0; double sum = 0.0; printf("Name Amount\n"); printf("----------- ------\n"); for (ctr=0; ctr<4; ctr++) { printf("%-10s %7.2f\n", names[ctr], amts[ctr]); sum += amts[ctr]; } printf("----------- ------\n"); printf("Total %7.2f\n", sum); return 0; } // I/O manipulators used to format output to file #include #include #include using std::cout; using std::fixed; using std::left; using std::ofstream; using std::right; using std::setprecision; using std::setw; int main(int argc, char* argv[]) { const char* names[] = { "Theodore", "Melinda", "Sasquatch", "Sue" }; double amts[] = { 37.5, 185.135, .2743, 409 }; int ctr = 0; double sum = 0.0; ofstream out("format3.txt"); if (out) { out << "Name Amount\n"; out << "----------- ------\n"; for (ctr=0; ctr<4; ctr++) { out << setw(10) << left << names[ctr] << " " << setw(7) << fixed << right << setprecision(2) << amts[ctr] << '\n'; sum += amts[ctr]; } out << "----------- ------\n"; out << "Total " << setw(7) << fixed << right << setprecision(2) << sum << '\n'; out.close(); } return 0; } // sprintf (and snprintf) used to format output to file #include #include using std::ofstream; int main(int argc, char* argv[]) { const char* names[] = { "Theodore", "Melinda", "Sasquatch", "Sue" }; double amts[] = { 37.5, 185.135, .2743, 409 }; char buf[300]; int ctr = 0; double sum = 0.0; ofstream out("format4.txt"); if (out) { snprintf(buf, 300, "Name Amount\n"); out << buf; snprintf(buf, 300, "----------- ------\n"); out << buf; for (ctr=0; ctr<4; ctr++) { snprintf(buf, 300, "%-10s %7.2f\n", names[ctr], amts[ctr]); out << buf; sum += amts[ctr]; } snprintf(buf, 300, "----------- ------\n"); out << buf; snprintf(buf, 300, "Total %7.2f\n", sum); out << buf; out.close(); } return 0; } // a simple tokenizer #include #include #include using std::cout; using std::string; int main(int argc, char* argv[]) { char linein[] = "first name,last name,1234.56:sales"; char* ptr; ptr = strtok(linein, ",:"); while (ptr != NULL) { cout << ptr << '\n'; ptr = strtok(NULL, ",:"); } return 0; } // token2.dat input file Theodore,37.5 Melinda,185.135 Sasquatch,.2743 Sue,409 // a tokenizer used to read fields from file records #include #include #include #include #include #include using std::cout; using std::fixed; using std::ifstream; using std::left; using std::right; using std::setprecision; using std::setw; using std::string; int main(int argc, char* argv[]) { double sum = 0.0; string line; char buf[300]; ifstream infile("token2.dat"); if (infile) { cout << "Name Amount\n"; cout << "----------- ------\n"; getline(infile, line); while (infile) { strncpy(buf, line.c_str(), 300); char* ptr = strtok(buf, ","); string name(ptr); ptr = strtok(NULL, ","); double amount = atof(ptr); cout << setw(10) << left << name << " " << setw(7) << fixed << right << setprecision(2) << amount << '\n'; sum += amount; getline(infile, line); } infile.close(); cout << "----------- ------\n"; cout << "Total " << setw(7) << fixed << right << setprecision(2) << sum << '\n'; } return 0; } // reading multiple fields from file records using iostream/ifstream #include #include #include #include #include #include using std::cout; using std::fixed; using std::ifstream; using std::left; using std::right; using std::setprecision; using std::setw; using std::string; int main(int argc, char* argv[]) { double sum = 0.0; string name; double amount; string strAmount; ifstream infile("token2.dat"); if (infile) { cout << "Name Amount\n"; cout << "----------- ------\n"; getline(infile, name, ','); while (infile) { getline(infile, strAmount); amount = atof(strAmount.c_str()); cout << setw(10) << left << name << " " << setw(7) << fixed << right << setprecision(2) << amount << '\n'; sum += amount; getline(infile, name, ','); } infile.close(); cout << "----------- ------\n"; cout << "Total " << setw(7) << fixed << right << setprecision(2) << sum << '\n'; } return 0; } // format3.txt and format4.txt output files Name Amount ----------- ------ Theodore 37.50 Melinda 185.13 Sasquatch 0.27 Sue 409.00 ----------- ------ Total 631.91