Click here to Skip to main content
12,635,630 members (26,497 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C
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:
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 7-Dec-12 23:20pm
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100