目录
1. iostream(I/O流)
1.1多组输入问题
2. fstream (文件流)
3.stringstream(字符串流)
1. iostream(I/O流)
需要注意:输入数据使用enter或空格进行分隔数据,scanf fscanf 等等。
1.1多组输入问题
在C语言中的多组测试用例输入:
int main()
{
char a[100];
while (scanf("%s",a)!=EOF)
{
}
return 0;
}
这段程序如何终止呢?需要ctrl+z(给一个结束信号)
对于cin有多组输入:也是ctrl+z结束。
#include#include using namespace std; int main() { string str; while (cin>>str)//这里是函数的调用operator(cin,str),它的返回值是istream& { } return 0; }
cin>>str,这里是函数的调用operator(cin,str),它的返回值是istream& ,平时使用while返回值都是整形什么的,这里返回值是一个对象的引用,那对象怎么做判断值的?
其实这里调用了一个函数重载:它会自动的把判断里的值转化为bool值
举例说明: 对象作为判断值
#include#include using namespace std; class B { public: operator bool()const { return _a != 0; } public: int _a; }; int main() { B b; while (b) //这里进行对象的判断,这里会去调用operator bool() ,b.operator() { cin >> b._a; } return 0; }
输入0就结束了:
2. fstream (文件流)
C语言的文件流:文本形式
#include#include using namespace std; struct ServerInfo { char _ip[32]; // ip int _port; // 端口 }; void testC_wtext()//写 { ServerInfo info = { "192.168.0.1", 2 }; FILE*fp = fopen("test.txt","w"); fprintf(fp,"%s %d",info._ip,info._port);//记得要以空格分开,fscanf是以空格或换行为分隔符的 fclose(fp); } void testC_rtext()//读 { ServerInfo info = {}; FILE*fp = fopen("test.txt", "r"); fscanf(fp,"%s%d",info._ip,&info._port); fclose(fp); printf("%s:%d", info._ip, info._port); } int main() { testC_wtext(); testC_rtext(); return 0; }
C++的文件流 ifstream/ofstream
二进制:
#include#include #include using namespace std; struct ServerInfo { char _ip[32]; // ip int _port; // 端口 }; class ConfigManager { public: ConfigManager(const char* s) :_filename(s) {} void WriteBinary(const ServerInfo& info) { //二进制方式打开写入 ofstream ofs(_filename.c_str(), ios_base::out|ios_base::binary); ofs.write((const char*)&info, sizeof(ServerInfo)); } void ReadBinary(ServerInfo& info) { //二进制方式打开读取 ifstream ifs(_filename.c_str(), ios_base::in | ios_base::binary); ifs.read((char*)&info, sizeof(ServerInfo)); } private: string _filename; }; int main() { ServerInfo info = {"hello world", 2 }; ServerInfo tmp; ConfigManager cm("1.txt"); cm.WriteBinary(info); cm.ReadBinary(tmp); cout << tmp._ip << " " << tmp._port << endl; return 0; }
文本:
#include#include #include using namespace std; struct ServerInfo { char _ip[32]; // ip int _port; // 端口 }; class ConfigManager { public: ConfigManager(const char* s) :_filename(s) {} //文本形式 void WriteText(ServerInfo& info) { ofstream ofs(_filename.c_str());//默认的就是文本 ofs << info._ip <<" "<< info._port; //写(这里空格不能少,做分割) ofs << info.d; } void ReadText(ServerInfo& info) { ifstream ifs(_filename.c_str());//默认就是文本 ifs >> info._ip >>info._port;//读 } private: string _filename; }; int main() { ServerInfo info1 = { "1234", 7}; ServerInfo tmp1; ConfigManager cm1("2.txt"); cm1.WriteText(info1); cm1.ReadText(tmp1); cout << tmp1._ip << " " << tmp1._port << endl; return 0; }
对于自定义类型:
首先看一下ostream 中重载运算符,对于内置类型使用的是ostream :发生了切片:子类ofstream对象传给了父类的引用。
再看继承关系:iostream和和ofstream和ostringstream 都继承了ostream
所以在重载自定义类型的数据的时候不能使用ofstream:
ofstream& operator<<(ofstream& ofs ,Date& d)
{
ofs << d._year << " " << d._month << " " << d._day;
return ofs;
}
这里会发生报错:内置类型的运算符函数重载返回的ostream,< 对于自定义类型的运算符重载: 这里也不能使用ostingstream:和上面继承是一样的问题。 对于自定义类型的数据和上面文本写是一样的共用一个:很方便啊。 对于自定义类型:#include
3.stringstream(字符串流)
#include
ostringstream& operator<<(ostringstream& out ,Date& d)
{
out << d._year <<" "<< d._month <<" "<< d._day << endl;
return out;
}
//自定义类型运算符重载
ostream& operator<<(ostream& out ,Date& d)
{
out << d._year <<" "<< d._month <<" "<< d._day << endl;
return out;
}
#include



