目录
一:IO流的继承关系:
二:输入输出功能
1. 基本用法
2. 格式化输入
3.非格式化输入
4. 格式化输出
三:流
1. 字符流
2. 向字符流中写入数据
3. 从字符流中读出数据
4. 清空字符流
5.完整的例子
四:文件流
一:IO流的继承关系:
类 | 含义 |
---|---|
basic_streambuf | 读取或写入数据 |
ios_base | 独立于字符类型的流属性 |
basic_ios | 依赖于字符类型的流属性 |
basic_istream | 用于读取数据的流基类 |
basic_iostream | 用于写入数据的流基类 |
basic_iostream | 用于读写数据的流基类 |
二:输入输出功能
typedef basic_istream<char> istream;
typedef basic_ostream<char> ostream;
1. 基本用法
#include <iostream>
int main(){
std::cout << "Type in your numbers";
std::cout << "(Quit with an arbitrary character): " << std::endl;
// 2000 <Enter> 11 <a>
int sum{0};
int val;
while (std::cin >> val) sum += val;
std::cout << "Sum: " << sum; // Sum: 2011
}
2. 格式化输入
include <iostream>int main()
{int a, b;std::cout << "Two natural numbers: " << std::endl;std::cin >> a >> b; // < 2000 11>std::cout << "a: " << a << " b: " << b;
}
3.非格式化输入
#include <iostream>int main()
{std::string line;std::cout << "Write a line: " << std::endl;std::getline(std::cin, line); // <Only for testing purpose.>std::cout << line << std::endl; // Only for testing purpose.std::cout << "Write numbers, separated by;" << std::endl;while (std::getline(std::cin, line, ';') ) {std::cout << line << " ";}
}
4. 格式化输出
#include <iostream>int main()
{int num{2011};std::cout.setf(std::ios::hex, std::ios::basefield);std::cout << num << std::endl; // 7dbstd::cout.setf(std::ios::dec, std::ios::basefield);std::cout << num << std::endl; // 2011std::cout << std::hex << num << std::endl; // 7dbstd::cout << std::dec << num << std::endl; // 2011
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>int main()
{std::cout.fill('#');std::cout << -12345;std::cout << std::setw(10) << -12345; // ####-12345std::cout << std::setw(10) << std::left << -12345; // -12345####std::cout << std::setw(10) << std::right << -12345; // ####-12345std::cout << std::setw(10) << std::internal << -12345; //-####12345std::cout << std::oct << 2011; // 3733std::cout << std::hex << 2011; // 7dbstd::cout << std::showbase;std::cout << std::dec << 2011; // 2011std::cout << std::oct << 2011; // 03733std::cout << std::hex << 2011; // 0x7dbstd::cout << 123.456789; // 123.457std::cout << std::fixed;std::cout << std::setprecision(3) << 123.456789; // 123.457std::cout << std::setprecision(6) << 123.456789; // 123.456789std::cout << std::setprecision(9) << 123.456789; // 123.456789000std::cout << std::scientific;std::cout << std::setprecision(3) << 123.456789; // 1.235e+02std::cout << std::setprecision(6) << 123.456789; // 1.234568e+02std::cout << std::setprecision(9) << 123.456789; // 1.234567890e+02std::cout << std::hexfloat;std::cout << std::setprecision(3) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::setprecision(6) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::setprecision(9) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::defaultfloat;std::cout << std::setprecision(3) << 123.456789; // 123std::cout << std::setprecision(6) << 123.456789; // 123.457std::cout << std::setprecision(9) << 123.456789; // 123.456789}
三:流
1. 字符流
//String stream for the input of data of type char and wchar_t.
std::istringstream and std::wistringstream//String stream for the output of data of type char and wchar_t.
std::ostringstream and std::wostringstream//String stream for the input or output of data of type char and wchar_t.
std::stringstream and std::wstringstream
2. 向字符流中写入数据
std::stringstream os;
os << "New String";
os.str("Another new String");
3. 从字符流中读出数据
std::string os;
std::string str;
os >> str;
str= os.str();
4. 清空字符流
std::stringstream os;
os.str("");
5.完整的例子
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>template <typename T>
T StringTo(const std::string& source) {std::istringstream iss(source);T ret;iss >> ret;return ret;
}template <typename T>
std::string ToString(const T& n) {std::ostringstream tmp;tmp << n;return tmp.str();
}int main()
{std::cout << "5= " << StringTo<int>("5"); // 5std::cout << "5 + 6= " << StringTo<int>("5") + 6; // 11std::cout << ToString(StringTo<int>("5") + 6); // "11"std::cout << "5e10: " << std::fixed << StringTo<double>("5e10"); // 50000000000
}
四:文件流
//File stream for the input of data of type char and wchar_t.
std::ifstream and std::wifstream//File stream for the output of data of type char and wchar_t.
std::ofstream and std::wofstream//File stream for the input and output of data of type char and wchar_t.
std::fstream and std::wfstream//Data buffer of type char and wchar_t.
std::filebuf and std::wfilebuf
#include <fstream>int main()
{std::ifstream in("inFile.txt");std::ofstream out("outFile.txt");out << in.rdbuf();
}
#include <fstream>
#include <iostream>
#include <istream>
#include <string>void writeFile(const std::string name) {std::ofstream outFile(name);if (!outFile) {std::cerr << "Could not open file " << name << std::endl;exit(1);}for (unsigned int i = 0; i < 10; ++i) {outFile << i << " 0123456789" << std::endl;}
}int main()
{std::string random{ "random.txt" };writeFile(random);std::ifstream inFile(random);if (!inFile) {std::cerr << "Could not open file " << random << std::endl;exit(1);}std::string line;std::cout << inFile.rdbuf();// 0 0123456789// 1 0123456789// 9 0123456789std::cout << inFile.tellg() << std::endl; // 200inFile.seekg(0); // inFile.seekg(0, std::ios::beg);std::getline(inFile, line);std::cout << line; // 0 0123456789inFile.seekg(20, std::ios::cur);std::getline(inFile, line);std::cout << line; // 2 0123456789inFile.seekg(-20, std::ios::end);std::getline(inFile, line);std::cout << line; // 9 0123456789
}
五:IO流运算符重载,支持用户自定义类型输入输出
friend std::istream& operator>> (std::istream& in, Fraction& frac);
friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
#include <fstream>
#include <iostream>
#include <istream>
#include <string>class Fraction {
public:Fraction(int num = 0, int denom = 0) :numerator(num), denominator(denom) {}friend std::istream& operator>> (std::istream& in, Fraction& frac);friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
private:int numerator;int denominator;
};
std::istream& operator>> (std::istream& in, Fraction& frac) {in >> frac.numerator;in >> frac.denominator;return in;
}
std::ostream& operator<< (std::ostream& out, const Fraction& frac) {out << frac.numerator << "/" << frac.denominator;return out;
}int main()
{Fraction frac(3, 4);std::cout << frac; // 3/4std::cout << "Enter two numbers: ";Fraction fracDef;std::cin >> fracDef; // <1 2>std::cout << fracDef; // 1/2}