项目中使用ifstream读取文件内容open函数中传递字符串参数的文件路径时,在windows下编译通过,但是在linux下编译失败.最后将std::string 转换为char * 传入open函数.
char *p;
p = (char *)malloc((str.lenth() + 1) * sizeof(char));
if(p == NULL)
{
return -1;
}
strcpy(p,str.c_str);
//以下为更好的方式
ifstream in("test.txt", 打开方式参数如ios::in);
if(in.is_open())
{
string strLine = "";
while(std::getLine(in, strLine))
{
if(strLine.empty())
{
continue;
}
//以下为字符串处理
}
}
in.close();