문제

이 코드를 참조하십시오.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
    int i = 0;
    FILE *fp;
    for(i = 0; i < 100; i++) {
        fp = fopen("/*what should go here??*/","w");
        //I need to create files with names: file0.txt, file1.txt, file2.txt etc
        //i.e. file{i}.txt
    }
}
도움이 되었습니까?

해결책

for(i = 0; i < 100; i++) {
    char filename[sizeof "file100.txt"];

    sprintf(filename, "file%03d.txt", i);
    fp = fopen(filename,"w");
}

다른 팁

사용 snprintf() ~와 함께 "file%d.txt" and파일 이름을 생성해야합니다.

들여다보다 snprintf.

char szFileName[255] = {0};
for(i = 0; i < 100; i++)
{
    sprintf(szFileName, "File%d.txt", i);
    fp = fopen(szFileName,"w");
}

이것은 작동해야합니다 :

for(i = 0; i < 100; i++) {
    char name[12];
    sprintf(name, "file%d.txt", i);
    fp = fopen(name, "w");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top