0

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.

2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by flageolet
0

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]);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.