hi again, i am having some dificulties replacing a certain line of a file.My problem in specific is writing to the file, a certain string, overwriting the line i want. However, my program just writes to the end of file ( as it should, according to the code ).
I will give you the code, along with examples:
file def:
Code:
im man
this strinks
the line i want to replace
i want to replace with chars ' '
my program, if he detects a certain string in the line, it is supposed to replace the line with chars ' ', if(name_esq == this), then replace the line that has this, like so:
however. it will do:
this is an excert of the program:
PHP Code:
#define MAXLINE[1024]
int main()
{
char line2[1024];
char *name_esq="this";
char *definitions="definitions.txt";
FILE * def
def=fopen(definitions, "r+");
while(fgets(line2, MAXLINE , def))
/* line2 - line of file def*/
{
printf("anything\n"); /* for debugging */
if(strstr(line2,name_esq)) /* if name_esq is in line2*/
{
printf("if works\n");/* for debugging */
printf("%s\n", line2); /* for debugging */
for(k=0; line2[k]!= '\n';k++)
{
line2[k]=' ';
fprintf(def,"%s\n",line2);
/*replaces line2's chars with ' '
*/
}
}/* I expect to replace the line in def,
which has the occurence of name_esq
but instead, it writes to the next line
as in the example.
*/
}
}
any sugestions will be gratefully accepted.
P.S. : if you think i overexplained, please warn me and i will try to rearrange it.
Thanks in advance