c - Naming a file with a loop counter -
how write line runs in loop , uses loop counter k give file name?
int k; for(k = 0; k < 10; k++) fopen("/home/ubuntu/desktop/" + k + ".txt", "w"); // java-like code
also how can create folder on local directory put files there instead of using desktop?
there 2 parts question: creating directory, , writing numbered files. try following (updated directory protection set explicitly, correct headers included, , 1 file closed before next 1 opened):
#include <stdio.h> #include <sys/stat.h> int main(void) { const char* mydirectory = "/users/floris/newdirectory"; char filename[256]; int ii, ferr; file *fp; ferr = mkdir(mydirectory, (mode_t)0700); for(ii=0; ii< 10; ii++) { sprintf(filename, "%s/file%d.txt", mydirectory, ii); if((fp = fopen(filename, "w"))!=null) { // whatever need } else { printf("could not open %s\n", filename); } fclose(fp); } return 0; }
Comments
Post a Comment