Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a normal text file as below:

-------------------
Debdipta
1234567 8
rajat
123432 4
Kaushik
685784 7
djghkvnfj
213122 6
--------------------

I want to delete a string from this file ex:below-

---------------
Debdipta
1234567 8
Kaushik
685784 7
djghkvnfj
213122 6
--------------------

You can see that rajat and its corresponding field is deleted:

I want to do this trough C?
I can file this string and put file pointer there through following code:
C#
fseek(fp, 0, SEEK_SET);
    while(!feof(fp))
    {
        fgets(strFileMem, MAX_PATH, fp);
        fseek(fp, 0, SEEK_CUR);
        if( NULL == strstr(strName, strFileMem) )
        {
            break;
        }
    }
    int ierr = fputs("\n", fp);
    ierr = fputs("\n", fp);



Any idea what to put in fputs???

any other code?

thanks
Posted
Comments
CHill60 8-Dec-12 12:00pm    
You could try reading the file line by line writing each line out to a new file unless it's the one you want to delete. Then when you're finished, delete the original file and rename the new one to the original name

 
Share this answer
 
fputs outpus a string to a file, it will not just eat a string.

You should read the whole file into a buffer and do the delete operation with a char array. Then output the modified string back to the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900