I want to create text files sequence like...
student1.txt
student2.txt
student3.txt
...
How to do it?
I have a sample code, but its not working for my problem.
#include<stdio.h>
void main()
{
FILE *fp;
int index;
for(index=1; index<4; index++)
{
fp=fopen("student[index].txt","w");
fclose(fp);
}
}
You are using aa fixed string "student[index].txt" rather than making a string with the number you want in it.
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);
}
}
You can't put variables inside of a string constant like that. You need to construct the string you want using sprintf
:
for(index=1; index<4; index++)
{
char name[20];
sprintf(name, "student%d.txt", i);
fp=fopen(name,"w");
fclose(fp);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.