Particular problem with a for loop

I have a particular problem with a for loop. This should write to a text file of the structures belonging to an ordered list in order to get a table.
Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct comp
{
    int code;
    char name[27];
    char des[52];
    int disp;
    struct comp *n;
};

struct comp *comps = 0, *p;

FILE *fw = fopen("miofile.txt", "w");

for(p = comps; p; p = p->n)
{
s1 = ((((float)(25 - strlen(p->name))) + 0.5) / 2);
s2 = ((((float)(25 - strlen(p->name))) - 0.5) / 2);
s3 = ((((float)(52 - strlen(p->des))) + 0.5) / 2);
s4 = ((((float)(52 - strlen(p->des))) - 0.5) / 2) - 1;
fprintf(fw, "|  %09.9i  ", p->code);
fprintf(fw, "| %*s%.25s%*s ", s1, " ", p->name, s2, " ");
fprintf(fw, "| %*s%.50s%*s ", s3, " ", p->des, s4, " "); fprintf(fw, "|    %09.9i    |\n", p->disp);
}

fclose(fw);




But the cycle continues indefinitely, and the output file size becomes huge, although there are written only a few structures (tested with notepad ++). The strange thing is that if I try to visualize the structures on the screen, the same cycle adapted to the purpose and ends properly and if I try to write to a file only the code and the availability of components, leaving out the name and description, in this If the loop ends correctly.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
for(p = comps; p; p = p->n)
{
s1 = ((((float)(25 - strlen(p->name))) + 0.5) / 2);
s2 = ((((float)(25 - strlen(p->name))) - 0.5) / 2);
s3 = ((((float)(52 - strlen(p->des))) + 0.5) / 2);
s4 = ((((float)(52 - strlen(p->des))) - 0.5) / 2) - 1;
fprintf(fw, "|  %09.9i  ", p->code);
// fprintf(fw, "| %*s%.25s%*s ", s1, " ", p->name, s2, " "); not compiled
// fprintf(fw, "| %*s%.50s%*s ", s3, " ", p->des, s4, " "); not compiled
fprintf(fw, "|    %09.9i    |\n", p->disp);
}


Any suggestion?
That's code for traversing a linked list. Unless the list created correctly, the code won't work correctly.
Topic archived. No new replies allowed.