鱼C论坛

 找回密码
 立即注册
查看: 3954|回复: 16

怎么从文件中册除指定的一行数据

[复制链接]
发表于 2013-5-13 02:10:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写了一个程序, 能向里面追加数据。
现在想增加一个功能, 就是在文件中查找与册除指定的数据。
查找已经写好了, 不过删除不知怎么写, 想问问, 请教一下
怎么从指定文件内删除某一行数据? 听说用memmove移覆盖,
之后再用truncate截断文件长度, 不过不知具体怎么实现。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-13 09:28:30 | 显示全部楼层
只能 重写
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-13 09:59:25 | 显示全部楼层
重写 !!!!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-13 11:05:47 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-13 14:02:05 | 显示全部楼层
都市小矮人 发表于 2013-5-13 11:05
必须重写!!!

什么重写? 重新写过程序?
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-13 21:49:07 | 显示全部楼层
来个详细指点一下吖~
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-13 21:56:55 | 显示全部楼层
1.把文件全部读到内存里
2.在内存里做增删改查
3.把改好的数据写入原文件并覆盖该文件
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-14 15:07:06 | 显示全部楼层
弱弱的问一下,你是用啥语言写的
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-14 16:30:46 | 显示全部楼层
楼主上个源代码呗
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-15 01:07:49 | 显示全部楼层
Skyline 发表于 2013-5-14 15:07
弱弱的问一下,你是用啥语言写的

C语言写呀。
这是C论坛, 当然是用C写呀
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-15 01:09:17 | 显示全部楼层
pcfate 发表于 2013-5-14 16:30
楼主上个源代码呗

不用一了吧。 看得明白。 现在在想板主说的方法要怎么写。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-15 09:53:20 | 显示全部楼层
我只是路过打酱油的。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-15 16:07:37 | 显示全部楼层
仰望天上的光 发表于 2013-5-13 21:56
1.把文件全部读到内存里
2.在内存里做增删改查
3.把改好的数据写入原文件并覆盖该文件

请问一下, 能具体一点吗?比如有少少的代码。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-15 20:15:35 | 显示全部楼层
喜欢散步 发表于 2013-5-15 16:07
请问一下, 能具体一点吗?比如有少少的代码。。。。。

用C写有点麻烦,“一点点代码”恐怕做不到。  用C++的STL可以吗?
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-15 21:15:46 | 显示全部楼层
仰望天上的光 发表于 2013-5-15 20:15
用C写有点麻烦,“一点点代码”恐怕做不到。  用C++的STL可以吗?

用C写一个可以吗?我还没有学C++, 呵呵
或者大概写一点代码就可以了。。。我想了几天, 做不出来。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-15 21:57:55 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-15 22:59:19 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define MAX_LEN 400
  5. struct Line {
  6.         char content[MAX_LEN];
  7.         struct Line* next;
  8. };

  9. struct MyFile {
  10.         struct Line* pHead;
  11.         struct Line* pTail;
  12. };
  13. void init( struct MyFile* pfile, const char* file_name );
  14. void write( struct MyFile* pfile, const char* file_name );
  15. void delete_line( struct MyFile* pfile, const char* line_content );
  16. void destroy( struct MyFile* pfile );

  17. int main (void) {
  18.         struct MyFile file;
  19.         init( &file, "C:\\data.txt" );
  20.         delete_line( &file, "cccc" );
  21.         write( &file, "C:\\data.txt" );
  22.         destroy( &file );
  23. }

  24. struct Line* getLine( FILE * pf ) {
  25.         struct Line* pLine = (struct Line*)malloc(sizeof(struct Line));
  26.         pLine->next = 0;
  27.         if( fgets( pLine->content, MAX_LEN ,pf ) ) {
  28.                 pLine->content[ strlen(pLine->content)-1 ]=0;//eat '\n'
  29.                 return pLine;
  30.         }else {
  31.                 free(pLine);
  32.                 return 0;
  33.         }
  34. }
  35. void init( struct MyFile* pfile, const char* file_name ) {       
  36.         FILE * pf = fopen( file_name, "r" );
  37.         struct Line* pline;
  38.         pfile->pHead = (struct Line*)malloc(sizeof(struct Line));//no use head
  39.         pfile->pTail = 0;
  40.         while( pline = getLine(pf) ) {
  41.                 if( pfile->pTail == 0 ) {
  42.                         pfile->pHead->next = pfile->pTail = pline;
  43.                 }else {
  44.                         pfile->pTail->next = pline;
  45.                         pfile->pTail = pline;
  46.                 }
  47.         }
  48.         fclose( pf );
  49. }

  50. void destroy( struct MyFile* pfile ) {
  51.         while( pfile->pHead != 0 ) {
  52.                 struct Line* ptmp = pfile->pHead;
  53.                 pfile->pHead = pfile->pHead->next;
  54.                 free( ptmp );
  55.         }
  56. }

  57. void write( struct MyFile* pfile, const char* file_name ) {
  58.         FILE * pf = fopen( file_name, "w" );
  59.         struct Line* pline = pfile->pHead->next;
  60.         while( pline ) {
  61.                 fprintf( pf, "%s\n", pline->content );
  62.                 pline = pline->next;
  63.         }
  64.         fclose( pf );
  65. }

  66. void delete_line( struct MyFile* pfile, const char* line_content ) {
  67.         struct Line* pline = pfile->pHead->next;
  68.         struct Line* next = pline->next;
  69.         int n;
  70.         while( next ) {
  71.                 if( (n = strcmp( line_content, next->content )) == 0 ) {
  72.                         pline->next = next->next;
  73.                         free(next);
  74.                         break;
  75.                 }
  76.                 pline = next;
  77.                 next = next->next;
  78.         }
  79. }
复制代码
先建立一个文件C:/data.txt内容可以如下:
aaaa
bbbb
cccc
dddd
eeee
执行后删除cccc那行
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-28 14:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表