我们经常会想自己输入一串字符,并创建它对应的txt文件,使用sprintf就可以实现
sprintf的第一个参数一定要是c风格的字符串
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n;
char str[100]="%d.txt";
for(n=1; n<=10; n++)
{
sprintf(str,"%d.txt",n);
ofstream ofs(str);
ofs.close();
}
return 0;
}
运行结果