string:
#include<strlib.h>
getline(cin,str);<span style="white-space:pre"> </span>//get the whole line include the space
realToString()<span style="white-space:pre"> </span>//change real to string
trim(); <span style="white-space:pre"> </span>//return a new string without space;
char:
<cctype>
isalpha()
isupper()
isdigit()
isspace() //'\t' '\n' '\f' '\v' '\r'
toupper()
tolower()
stream:
获取文件流
get() put()
获取文件名
int next;
ifstream infile(filename);
while((next=infile.get())!=EOF){
char nexzc=char(next);
cout.put(nexzc);
}
void getFileName(ifstream &infile){
while(true){
cout<<"input filename:"<<endl;
string name;
getline(cin,name); //get whole line
infile.open(name.c_str());
if(!infile.fail())return ; //如果获取失败
infile.clear(); //清除文件流
cout<<"unable to access the file try again"<<endl;
}
}
ifstream直接转化为 integer
直接使用文件流读取数据不同于 getline 读取到空格会算作下一次读取
ifstream infile(filename);
if(infile.fail())
cerr<<"can't open"<<endl;
int score;
while(true){
infile>>score;
if(infile.fail()||!infile.eof())break;
}
getline() cout
ifstream infile;
infile.open(filename.c_str());
while(true){
getline(infile,line);
if(infile.fail())break;
cout<<line<<endl;
}
//string to integer
int sti(string str){
int value;
istringstream stream(str);
stream>>value>>ws;//读入 value ws:格式控制 无论后面与多少空格
if(stream.fail()||stream.eof()){
cerr<<"illegal integer";
}
return value;
}
输入输出: 获取一个整型
int getinteger(void){
string line;
int value;
while(true){
getline(cin,line);
istringstream stream(line);
stream>>value>>ws;
if(!stream.fail()&&!stream.eof())break;
cout<<"error: some format is illegal";
}
return value;
}
复制流
copystream(infile,cout);
void copystream(istream & is, ostream & os) {
while (true) {
int ch = is.get();
if (ch == EOF) break;
os.put(ch);
}
}
小数点转换 四舍五入 利用流
<iomanip>
<sstream>
double digit(double value,int sign){
stringstream str;
str<<setprecision(sign)<<value;
str>>value;
return value;
}
int 转化为 string
string intToString(int x){
stringstream ss;
string str;
ss<<x;
ss>>str;
return str;
}
部分代码从 programming abstraction in c++ 转载