// Demonstrate file streams (fstream) string streams (sstream) // and IO Manipulators // George F. Riley, Georgia Tech, Fall 2009 #include #include #include #include using namespace std; int main() { int i = 99; double d = 1234.5678; char* s = "Test String"; // Use various io manipulators to affect the appearance of outputs // First the default cout << "i " << i << " d " << d << " s " << s << endl; // Width of 10 using setw cout << "i " << setw(10) << i << " d " << d << " s " << s << endl; // Width of 10 using width cout.width(10); cout << "i " << i << " d " << d << " s " << s << endl; // Left Justivied cout << "i " << setw(10) << left << i << " d " << endl; // Demonstrate the format flags // Allowable values are: //ios::boolalpha // input/output bool objects as alphabetic names (true, false). //ios::dec // input/output integer in decimal base format. //ios::fixed // output floating point values in fixed-point notation. //ios::hex // input/output integer in hexadecimal base format. //ios::internal // the output is filled at an internal point enlarging the output up to the field width. //ios::left //ios::oct //ios::right //ios::scientific //ios::showbase //ios::showpoint //ios::showpos //ios::skipws //ios::unitbuf //ios::uppercase ios::fmtflags orig = cout.flags(); cout.flags(ios::hex | ios::showbase | ios::showpos | ios::scientific); cout << "i " << i << " d " << d << " s " << s << endl; // Note that flags are persistent. cout << "i " << i << " d " << d << " s " << s << endl; // Set back to original cout.flags(orig); cout << "i " << i << " d " << d << " s " << s << endl; // Demonstrate fill character cout.fill('*'); cout << setw(10) << left << i << endl; // Demonstrate setf and unsetf cout.setf(ios::showpos); cout << i << " " << i << " " << i << " "; cout.unsetf(ios::showpos); cout<< i << endl; // Demonstrate floating point precision and fixed streamsize origPrecision = cout.precision(1); cout << " d " << fixed << d << " " << d << " "; cout.precision(origPrecision); cout << d << endl; // Demonstrate ifstream and ofstream ifstream inFile; // Input file ofstream ofFile; // Output file char buf[100]; // Buffer for reading/writing inFile.open("fstream.cc", ios::binary); // Open for input // The ! operator can be used to test successful open if (!inFile) { cout << "Can't open fstream.cc" << endl; exit(1); } ofFile.open("newfstream.cc", ios::binary); // Open for output int total = 0; // Total bytes read while(inFile.good()) { // Read from inFile inFile.read(buf, sizeof(buf)); // gcount() returns number of bytes on last read int actual = inFile.gcount(); total += actual; ofFile.write(buf, actual); // Write on output file } // Close the files inFile.close(); ofFile.close(); cout << "Total read/written " << total << endl; } // Output from this program is: // i 99 d 1234.57 s Test String // i 99 d 1234.57 s Test String // i 99 d 1234.57 s Test String // i 99 d // i 0x63 d +1.234568e+03 s Test String // i 0x63 d +1.234568e+03 s Test String // i 99 d 1234.57 s Test String // 99******** // +99 +99 +99 99 // d 1234.6 1234.6 1234.567800 // Total read/written 3399