c - 如何在 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);
}
}
解决方案
您正在使用固定字符串“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);
}
}
推荐阅读
- batch-file - 如何使用 set/p 读取批处理文件并响应文本输入
- xslt-2.0 - XSLT 问题 - 将多列转置为行
- windows - 在 MSI 包中设置 UI 级别
- javascript - 如何为网格容器内的 div 添加平滑替换?
- java - Java - 运行应用程序时 IntelliJ 未找到 module-info.java 中所述的模块库
- android - 在 Koltlin 中膨胀类片段时出错
- c# - 在 C# 中查找键值对的快速方法
- bash - Stata 没有看到 ODBC 连接所需的环境变量
- jsf - Primefaces 在 DOM 中复制元素
- python - 是否可以在 PySpark 中对 DataFrame 进行去标记化?