Hi I was hoping if anyone could help me figure out what im doing wrong. I wanted to delete a specific line in a text file using fputs...I dont know why it isn't writing on the textfile in other cases it only writes in the end of the file..'

heres the code...I really appreciate any help...

#include <stdio.h>

#include <string.h>

int main(void)



{

   FILE * pFile;

   

char mystring [10000];

char w[10000];

char t[10];

int i,a=0,s=0;

printf("Enter word to search\n");

gets(t);

  pFile = fopen ("dumpbeta.txt" , "r+");

     if (pFile == NULL) 

     printf("Error opening file");

     else

     {

     while(fgets (mystring,10000 ,pFile))

     {

	a++;



     for(i = 0; mystring[i] != '\n' && mystring[i] != EOF;i++);

      mystring[i] = '\0';



if(s==0)    

{



strcpy(w,mystring); 

strtok(mystring,"@");   



}



if(strcmp(mystring,t)==0)

s=a;



}



    

}

     

    if(s==0)

    printf("No search found!");

    else

    { 

     

     char *result = NULL;

     result = strtok( w,"@" );

     while( result != 0) {

    result = strtok( 0,"@" );

    

   

     }

    

 

}

 fclose(pFile);

return 0;

}

In order to deleted text from a file you have to rewrite the entire file but omitting the text to be deleted. you can't just delete text in the original file.

In order to deleted text from a file you have to rewrite the entire file but omitting the text to be deleted. you can't just delete text in the original file.

thanks for the quick reply..could you please give me some reference or example how to do that? please..:'(

open the original file for reading
open a new temporary file for writing
for each line in the original file
   if this does not contain the text to be deleted 
           then write the line out to the output file
end of loop
close both files
delete the original file
rename the temp file to the name of the original file
open the original file for reading
open a new temporary file for writing
for each line in the original file
   if this does not contain the text to be deleted 
           then write the line out to the output file
end of loop
close both files
delete the original file
rename the temp file to the name of the original file

thanks a lot!!!it really helped

That one is good!.
but to do it more easier and just accessing on one OPEN file. you can do this:

1. Copy all the text on the file. i know you how to use STRCPY
2. Do the your search function for a text you want to be deleted.
3. Then do a comparing function. STRCMP and (STRCPY - "The char variable you use in inputting the text to be deleted!")

HOPE that HELPS...
you can E-mail me for more questions men!
<email removed>>

Edited 5 Years Ago by WaltP: Do not post email addresses on forums. Or do you love getting spam?

Comments
bump of two-year-old thread
This article has been dead for over six months. Start a new discussion instead.