Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom filenames in C?

Please see this piece of code:

#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
    }
}
like image 542
Lazer Avatar asked Mar 15 '10 06:03

Lazer


People also ask

How do I create a custom file name?

To change the name of a file in Windows, simply go to the folder list and right-click on the file or folder you want to rename. You will then see a menu that includes Rename; this allows you to rename the document.

How do you name a file in C++?

Include files in C++ always have the file name extension ". hh". Implementation files in C++ always have the file name extension " . cc ".


1 Answers

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

    sprintf(filename, "file%03d.txt", i);
    fp = fopen(filename,"w");
}
like image 197
caf Avatar answered Sep 30 '22 16:09

caf