#include <iostream> using namespace std; int main() { char ch; cout << "Enter a String :" << endl; // 输入:abcd cin.get(ch);// 取出输入流中第一字符 ch = a, 输入流中剩余:bcd cout << "ch = " << ch << endl; cin.get(ch);// 取出输入流中第一字符 ch = b, 输入流中剩余:cd cout << "ch = " << ch << endl; cin.putback(ch); // 将ch =b , 回退到输入流中,输入流中剩余:bcd cin.get(ch);// 取出输入流中第一字符 ch = b, 输入流中剩余:cd cout << "ch = " << ch << endl; ch = cin.peek(); // 尝试读取输入流中的第一字符,单并没有取走,输入流中剩余:cd cout << "ch = " << ch << endl; cin.get(ch);// 取出输入流中第一字符 ch = c, 输入流中剩余:d cout << "ch = " << ch << endl; return 0; }
#include <iostream> using namespace std; int main() { int a = 23; int b = 34; cout << "Enter a number followed by a character:" << endl; cin >> a >> b; cout << "a="<< a << ",b=" << b << endl; cin.clear();// 将输入流变为正常状态,没有此句会直接结束 cin.ignore(200,‘\n‘); cout << "Enter two numbers:" << endl; cin >> a >> b; cout << "a="<< a << ",b=" << b << endl; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { double x, y, z; x = 15.572; y = 1234.43; z = 9823.2385; cout << fixed << showpoint; cout << setprecision(2) << "setprecision(2)" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; cout << setprecision(3) << "setprecision(3)" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; cout << setprecision(4) << "setprecision(4)" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; cout << setprecision(3) << x << " " << setprecision(2) << y << " " << setprecision(4) << z << endl; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { int x = 19; int a = 345; double y = 76.384; cout << fixed << showpoint; cout << "12345678901234567890" << endl; cout << setw(5) << x << endl; cout << setw(5) << a << setw(5) << "Hi" << setw(5) << x << endl << endl; cout << setprecision(2); cout << setw(6) << a << setw(6) << y << setw(6) << x << endl; cout << setw(6) << x << setw(6) << a << setw(6) << y << endl; cout << setw(5) << a << x << endl; cout << setw(2) << a << setw(4) << x << endl; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { int x = 19; int y = 7624; cout << "12345678901234567890" << endl; cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; cout.fill(‘*‘); cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; cout << setw(5) << x << setw(7) << setfill(‘#‘) << y << setw(8) << "warm" << endl; cout << setw(5) << setfill(‘@‘) << x << setw(7) << setfill(‘#‘) << y << setw(8) << setfill(‘^‘)<< "warm" << endl; cout.fill(‘ ‘); cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { int x = 19; int y = 7624; cout << left; cout << "12345678901234567890" << endl; cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; cout.fill(‘*‘); cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; cout << setw(5) << x << setw(7) << setfill(‘#‘) << y << setw(8) << "warm" << endl; cout << setw(5) << setfill(‘@‘) << x << setw(7) << setfill(‘#‘) << y << setw(8) << setfill(‘^‘)<< "warm" << endl; cout.unsetf(ios::left); cout.fill(‘ ‘); cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl; return 0; }
#include<fstream> #include<stream> using namespace std; int main() { fstream fin("data.txt"); //打开文件 string ReadLine; while(getline(fin,ReadLine)) //逐行读取,直到结束 { ... } fin.close(); return 0 }
原文:http://blog.csdn.net/haifengzhilian/article/details/23996245