C++IO流
C++IO流分为三种 iostream fstream sstream
iostream
就是我们平时使用的普通输入输出(cin,cout,cerr,clog)
void TestIostream() {
int i;
cin >> i;
cout << i << endl;
}
fstream
文件的输入输出 包括ifstream 和 ofstream
C++根据文件内容的数据格式分为二进制文件和文本文件。
向文件中写入字符串
void TestFstream() {
ofstream o("test.txt");
o << "test << " << endl;
o.close();
}

把字符串从文件中读出
void TestFstream() {
ifstream ifs("test.txt");
char c[20];
ifs.getline(c, 19);
cout << c << endl;
}

sstream
字符串的输入与输出
#include<iostream>
#include<sstream>
using namespace std;
struct ServerInfo
{
char _ip[20];
int _port;
};
int main()
{
ServerInfo info = { "127.0.0.1",80};
stringstream sm;
sm << info._ip << " " << info._port;
string buffer = sm.str();
cout << buffer << endl;
}

stringstream sm;
sm.str("127.0.0.1 80");
ServerInfo rinfo;
sm >> rinfo._ip >> rinfo._port;

|