fprintf将std::string类型写入文件

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());

猜你喜欢

转载自blog.csdn.net/xjcwzp/article/details/83104915
今日推荐