啥也不说,直接上代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs("./conf.txt"); //定义输入流对象
if(!ifs) {
cerr<< "ifs create failed!" <<endl;
}
string ip;
char temp[50] = {0};
ifs.getline(temp,20,'='); //读一行,将内容存入temp,遇到 = 结束;
ifs >> ip; //此处的开始位置为上次的结束位置,将流内容写入ip字符串,输入流遇到回车换行符 '\n'结束;
cout<<"ip = "<<ip<<endl;
unsigned short port;
ifs.getline(temp,20,'=');
ifs >> port;
cout << "port = "<< port <<endl;
string usrname;
ifs.getline(temp,20,'=');
ifs >> usrname;
cout << "usrname = " << usrname <<endl;
string passport;
ifs.getline(temp,20,'=');
ifs >> passport;
cout << "passport = " << passport <<endl;
ifs.close();
return 0;
}
1) istream& getline (char* s, streamsize n );
stream& getline (char* s, streamsize n, char delim )
用法:ifs.getline(str, n , delim);
功能: 从输入流中读入字符,存到str变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束