I want to create several files at once containing the same data. Is this possible?

I tried using a variable in the file pointer and file name:

int tel; 
  
   for (tel = 0; tel < 200; tel++)
   {
          
   
   FILE *bestand_patienten[tel] = fopen("patienten/patientent[tel].txt", "w");
   fprintf (bestand_patienten[tel], "hallo");
  
 }

Didn't work.

Recommended Answers

All 2 Replies

your code is attempting to open the same file for writing multiple times. You can't do that because the same file can be opened for writing by only one program at a time -- write mode requires exclusive use of the file. What you want to do is create an array of 200 filenames (or however many you want). There are better ways of generating the filenames, but I used this one for simplicity.

char *filenames[] = {
  "patienten/patientent1.txt",
  "patienten/patientent2.txt",
  ...
};

int tel; 
FILE *bestand_patienten[200] = {0};  
for (tel = 0; tel < 200; tel++)
{         
bestand_patienten[tel] = fopen(filenamest[tel], "w");
fprintf (bestand_patienten[tel], "hallo");
 
}

// now close all those files
for (tel = 0; tel < 200; tel++)
{         
   fclose(bestand_patienten[tel]);
}

works like a charm

thx a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.