C++在循环中动态创建文件

用到了数值类型转string类型的函数 to_string,包括在头文件中,

参考了 https://www.cnblogs.com/johngu/p/7878029.html

用到了string类型转char*类型的函数c_str,是string类型的成员函数,

参考了 https://blog.csdn.net/suan2014/article/details/95320436

#include
#include
#include
using namespace std;

int main()
{
	errno_t err;
	FILE* fp;
	string str;
	const char* str1;
	for (int i = 0; i < 20; i++)
	{
		str = "aaaaaaaaaaaaaaaa" + to_string(i) + ".txt";
		str1 = str.c_str();
		cout << str << endl;
		if (err = fopen_s(&fp, str1, "wb"))
		{
			cout << "open faild." << endl;
			exit(-1);
		}
		fputc('c', fp);
		fclose(fp);
	}
}

 

你可能感兴趣的:(C++)