How to create custom file names in C?

Please view this code snippet:

#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 //ie file{i}.txt } } 
+4
source share
5 answers
 for(i = 0; i < 100; i++) { char filename[sizeof "file100.txt"]; sprintf(filename, "file%03d.txt", i); fp = fopen(filename,"w"); } 
+10
source
 char szFileName[255] = {0}; for(i = 0; i < 100; i++) { sprintf(szFileName, "File%d.txt", i); fp = fopen(szFileName,"w"); } 
+1
source

This should work:

 for(i = 0; i < 100; i++) { char name[12]; sprintf(name, "file%d.txt", i); fp = fopen(name, "w"); } 
0
source

Use snprintf() with "file%d.txt" and i` to generate the file name.

0
source

Take a look at snprintf .

0
source

Source: https://habr.com/ru/post/1304119/


All Articles