I use the following code to insert array of structure to file but it crash:
void SaveInFile(List * pl)
{
int i;
int s = ListSize(pl);
file = fopen("myFile.txt", "w"); //3shan aktb 3la file mn gded
for (i = 0; i <= s; i++) {
file = fopen("myFile.txt", "a");
fprintf(file, "IDOfprocess%s/n", pl->entry[i].ID);
fprintf(file, "IDOfprocess%s/n", pl->entry[i].BurstTime);
}
fclose(file);
}
Any idea how to solve this?
ListSizereturns the size N for list with N elements, then:for( i=0;i<=s;i++)→for( i=0;i<s;i++), otherwise you'll be getting segmentation fault.fopen()inside the loop (i.e., for each entry). Remove the secondfopen(). Also, what data types areIDandBurstTime? You are using %s which implies null-terminated string, so hopefully they are indeed strings.fopenstimes withoutfclose... don't do that oh, and check theFILE *returned byfopen, too. If it'sNULL, something went wrong