#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream a;
a.open("c.txt"); //file contains "afe 21 3 df"
string b;
while (a>>b)
{
if (atoi(b)>0) cout<<b;
}
}
error是cannot convert string to constant *char, 同时,我这样check是否是整数是正确的吗
这样check是否是整数是不正确的
举个例子:
int main()
{
string s = "12a";
int k = atoi(s.c_str());
printf("%d",k);
return 0;
}
首先string用atoi时,写成atoi(s.c_str()),将string先转换为char *
其次当s开头是数字后面是英文时,atoi会转换字符串开头的数值,不会是0。比如atoi("12a")的结果是12,不是0。因此你的代码不能用来判断字符串是不是整数