首页 > 解决方案 > 如何在 C 中创建文本文件序列

问题描述

我想创建文本文件序列,如...

student1.txt
student2.txt
student3.txt
...

怎么做?

我有一个示例代码,但它不适用于我的问题。

#include<stdio.h>

void main()
{
    FILE *fp;
    int index;

    for(index=1; index<4; index++)
    {
        fp=fopen("student[index].txt","w");
        fclose(fp);
    }
}

标签: cfilesequencefopenfclose

解决方案


您正在使用固定字符串“student[index].txt”,而不是使用您想要的数字制作一个字符串。

void main()
{
  FILE *fp;
  int index;
  char fname[100];

  for(index=1; index<4; index++)
  {
    sprintf(fname, "student%d.txt", index);
    fp=fopen(fname,"w");
    fclose(fp);
  }
}

推荐阅读