FILE* pf = NULL;
char filename[10][20] = { "student1","student2","student3","student4","student5","student6","student7","student8","student9","student10"};
for(int i = 0;i < 10;++i)
{
fopen_s(&pf,filename[i],"wb");
char buf[5] = {};
itoa(i,buf,10);
fwrite(buf,1,5,pf);
fclose(pf);
}
system("pause");
//下面是运行效果 。如果觉得满意记得采纳。还有要说的是 。只能创建文件 不能删除。但是可以清除某个文件的数据。
我的程序如下:
/* file_test.c BY furzoom @2015-11-25 */
#include
#include
#define LENGTH 32
void delete_file(int n);
void create_file(int n);
int main(int argc, char *argv[])
{
int n, i;
char filename[LENGTH];
if (argc != 2)
{
printf("Usage: %s [c|d]\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "c") == 0)
create_file(10);
else if (strcmp(argv[1], "d") == 0)
delete_file(10);
else
printf("Usage: %s [c|d]\n", argv[0]);
return 0;
}
void delete_file(int n)
{
int i;
char filename[LENGTH];
for (i = 0; i < n; i++)
{
snprintf(filename, LENGTH, "Student%d", i);
remove(filename);
}
}
void create_file(int n)
{
int i;
char filename[LENGTH];
for (i = 0; i < n; i++)
{
snprintf(filename, LENGTH, "Student%d", i);
fclose(fopen(filename, "w"));
}
}
生成文件使用参数'c':
./a.out c
删除文件使用参数'd':
./a.out d
创建用循环创建
删除用循环删除