0

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?

4
  • 2
    If ListSize returns 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. Commented Jan 8, 2014 at 14:33
  • 3
    You're calling fopen() inside the loop (i.e., for each entry). Remove the second fopen(). Also, what data types are ID and BurstTime? You are using %s which implies null-terminated string, so hopefully they are indeed strings. Commented Jan 8, 2014 at 14:35
  • 1
    1) fopen the file first in "a" mode, unless you want to "truncate" it, but anyway you can't open it s+1 time times, thus 2) remove fopen from inside the lloop Commented Jan 8, 2014 at 14:48
  • ouch... I bet you any money this will leak memory... calling fopen s times without fclose... don't do that oh, and check the FILE * returned by fopen, too. If it's NULL, something went wrong Commented Jan 8, 2014 at 15:13

2 Answers 2

1

You are opening the file multiple times without closing it. This will do:

void SaveInFile(List* pl)
{

int i;
int s=ListSize(pl);

file=fopen("myFile.txt","w");//3shan aktb 3la file mn gded 
fclose(file);
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);
}
}

If you do not close the file, the content of any unwritten output buffer is not written to the file.

But what you should actually do is open the file one time and perform the append operations:

void SaveInFile(List* pl)
{

int i;
int s=ListSize(pl);

file=fopen("myFile.txt","w");//3shan aktb 3la file mn gded 
fclose(file);

file=fopen("myFile.txt","a");
for( i=0;i<=s;i++){

fprintf(file,"IDOfprocess%s/n",pl->entry[i].ID);
fprintf(file,"IDOfprocess%s/n",pl->entry[i].BurstTime);
}
fclose(file);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You still need to close the first fopen or you have a resource leak... may be just comment it.
how to write new line as \n print the same charachter not new line
1

your for loop is reaching s and you are starting from 0 (so you are treating s+1 elements and not s elements)

So it should be

for( i=0;i<s;i++){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.