int main(int argc, char *argv[])
{
std::cout << "Welcome to the QNX Momentics IDE" << std::endl;
std::string logtxt = "test";
FILE * fp = NULL;
fp = fopen( "/root/log1017", "a+" );
if ( fp == NULL )
return -1;
int res=fprintf( fp, "%s\n" ,logtxt.c_str());
//int res=fprintf( fp, "111\n");
fclose(fp);
std::cout <<logtxt<< std::endl;
return 1;
}
C语言中使用的字符串是以’\0‘字符为结束符。
可以调用其成员函数c_str(),来将string类型的对象转成C风格的字符串。
fprintf( fp, "%s\n" ,logtxt.c_str());