C语言 批量产生文件名.pdf
程序中有时候,我们需要生成多个文件名连续的文件,那么我们就需要用到函数
sprintf, swprintf
int sprintf( char *buffer, const char *format [, argument] ... );
int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );
Routine Required Header Compatibility
sprintf ANSI, Win 95, Win NT
swprintf or ANSI, Win 95, Win NT
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
sprintf returns the number of bytes stored in buffer, not counting the terminating null
character. swprintf returns the number of wide characters stored in buffer, not counting
the terminating null wide character.
Parameters
buffer
Storage location for output
format
Format-control string
argument
Optional arguments
批量生成文件名为 Beauty01.bmp,Beauty02.bmp,Beauty03.bmp
/*批量生成文件名为 Beauty01.txt,Beauty02.txt,Beauty03.txt
内容写文件名*/
#include
#include
int main()
{
unsigned char file_name[15];
unsigned i;
for(i=1;i<=9;i++)
{
sprintf(file_name,"Beauty%02d.txt",i);
freopen(file_name,"wb",stdout);
printf("%s\n",file_name);
}
return 0;
}