how to re-write each line in text file easily way

hi, im trying to make a function which could (in for statmnt) read format from file, delete same line and write there data from stdin...

i was thinking how to do this . . .but my only idea was to read it all to array, open file with deleting hole file, and write there new data from stdin.
could you somehow help me with doing this ?

i am posting function which may do this :}, but this function isn't working, problem is in fgets

file format is like
int, int
int, int


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
26
27
28
29
30
31
32
33
static void dopln_zasobnik()
{
FILE *f;
int i,pocet_m, pr=0;
char zap[12][12];
printf("zacala f-cia\n");
fopen("zasobnik.txt","r");
Topen(f);
printf("zacina cyklus\n");
for (i=1;i<13;i++)
	{
	if (pr==0) 
	{
		printf("ak je pr 0 zapise %d\n",i);
		fgets(zap[i],4,f);
		printf("nacitalo zo surobu %s %d\n",zap[i],i);
	}
		if (i==12 && pr==0) 
		{
		pr=1;
		i=1;
		fopen("zasobnik.txt","w");
		Topen(f);
	}
	if (pr==1)
	{
	printf("zadaj pocet pre %d-ty riadok\n",i);
	scanf("%d",&pocet_m);
	fprintf(f,"%s %d\n",zap[i], pocet_m);
	}
}
Tclose(f);
}
i is too big for zap! zap is 0-11. i is 1-12. when i==12, error!

line 5:
char zap[13][12];

this is a popular problem tonight.

is there still a problem in fgets?
for (i=1;i<13;i++) should be for (i=0;i<12;i++)
[code]for (i=1;i<13;i++) should be for (i=0;i<12;i++)[code]

sure, but then you have to change other lines to fit.

in line 18. variable i never gets to 12. and in line 21, he resets i to 1 instead of 0.
does he have to close the file before opening it again for writing on line 22? i guess it is good manners.
Topic archived. No new replies allowed.