使用fgets删除文件中的字符串 [英] Delete a string in a file using fgets

查看:75
本文介绍了使用fgets删除文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个普通的文本文件如下:



-------------------

Debdipta

1234567 8

rajat

123432 4

Kaushik

685784 7

djghkvnfj

213122 6

------------------- -



我想从这个文件中删除一个字符串ex:下面 -



---- -----------

Debdipta

1234567 8

Kaushik

685784 7

djghkvnfj

213122 6

--------------------



您可以看到rajat及其相应字段已被删除:



我想通过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

推荐答案

请参阅以下链接

http://forums.devshed。 COM / C编程-42 /删除线-从-A-文件在-C-360169.html 。可能对你有帮助
Refer the link below
http://forums.devshed.com/c-programming-42/deleting-lines-from-a-file-in-c-360169.html. may be it is help ful for you


fputs将一个字符串输出到一个文件,它不会只吃一个字符串。



您应该将整个文件读入缓冲区并使用char数组执行删除操作。然后将修改后的字符串输出回文件。
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.


这篇关于使用fgets删除文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆