C 语言 删除文件中的一行

ohaiyou 2010-05-20 04:44:30
比如有个文件内容
01:aaaaaaaaaa
02:bbbbbbbbb
03:cccccccccccc
04:ddddddddddd
05:eeeeeeeeee


怎么根据03这个行号来删除那一行呢? 变成如下:
01:aaaaaaaaaa
02:bbbbbbbbb
04:ddddddddddd
05:eeeeeeeeee

思路:先用fseek找到要删除的记录的位置,然后将此位置之前的记录重新写

入一个文件,然后再将文件指针移到此记录的末尾处,在将余下的记录也写入

刚才写入的文件中,最后删除原来的文件,重新命名现在文件啊.


思路是有,可是我是新手,编程组织不起来,哪位高手可以帮帮我呀
...全文
2186 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zrwzlcy 2011-11-17
  • 打赏
  • 举报
回复
12楼 老霸道了···
gukesdo 2011-10-21
  • 打赏
  • 举报
回复
12楼精巧,学习了!
invail 2010-05-22
  • 打赏
  • 举报
回复
学习
qq774097676 2010-05-22
  • 打赏
  • 举报
回复
进来学习一下
旭子 2010-05-21
  • 打赏
  • 举报
回复
局部择优
zhh_kv 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 jernymy 的回复:]

C/C++ code


#include "stdio.h"
#include "string.h"

#define DST_FILE "dst.txt"
#define SRC_FILE "src.txt"
#define MAX_LEN 256

static char achBuf[MAX_LEN] = {0};

static int RemoveSameString……
[/Quote]
不错!
yiruirui0507 2010-05-20
  • 打赏
  • 举报
回复
有字符串匹配是不是好点?
renbin5566 2010-05-20
  • 打赏
  • 举报
回复
很有用,学习了
jernymy 2010-05-20
  • 打赏
  • 举报
回复
/*
生成test.exe,同时在此目录下编译一个
exec.bat内容
test.exe aaa.txt 03:

同时此目录下要有对应的aaa.txt内容:
01:aaaaaaaaaa
02:bbbbbbbbb
03:cccccccccccc
04:ddddddddddd
05:eeeeeeeeee

运行exec.bat则会生成dst.txt内容为更改后的文件
01:aaaaaaaaaa
02:bbbbbbbbb
04:ddddddddddd
05:eeeeeeeeee
*/
jernymy 2010-05-20
  • 打赏
  • 举报
回复


#include "stdio.h"
#include "string.h"

#define DST_FILE "dst.txt"
#define SRC_FILE "src.txt"
#define MAX_LEN 256

static char achBuf[MAX_LEN] = {0};

static int RemoveSameStringLine(int argc, char **argv)
{
FILE *fpSrc = NULL;
FILE *fpDst = NULL;
char achStr[50] = {0};
int nCount = 0;
int nArgc = 0;
char achSrcF[50] = {0};
#if 1
if (argc < 2)
{
printf("parameter is less than < 2\n");
return -1;
}

strcpy(achSrcF, argv[1]);
strcpy(achStr, argv[2]);
printf("argc: %d\n", argc);
for (nArgc = 0; nArgc < argc; nArgc++)
{
printf("argv[%d]: %s\n", nArgc, argv[nArgc]);
}

for (nArgc = 3; nArgc < argc; nArgc++)
{
strcat(achStr, " ");
strcat(achStr, argv[nArgc]);
}

#else
strcpy(achSrcF, SRC_FILE)
strcpy(achStr, SEEK_STR);
printf("argv:[0]-%s,[1]-%s,[2]-%s\n", argv[0], SRC_FILE, SEEK_STR);
#endif

printf("File:%s find str:\"%s\", and remove this str line\n", achSrcF, achStr);

fpSrc = fopen(achSrcF, "rt");
if (NULL== fpSrc)
{
printf("Open source file: %s failed\n", SRC_FILE);
return -1;
}

fpDst = fopen(DST_FILE, "wt");
if (NULL== fpDst)
{
printf("Create source file: %s failed\n", DST_FILE);
fclose(fpSrc);
return -1;
}

nCount = 0;

while (!feof(fpSrc))
{
memset(achBuf, 0, sizeof(achBuf));
fgets(achBuf, sizeof(achBuf), fpSrc);
if (NULL == strstr(achBuf, achStr))
{
fputs(achBuf, fpDst);
}
else
{
nCount++;
}
}
fclose(fpSrc);
fclose(fpDst);
printf("Total Count Same string:%d\n", nCount);

return 0;
}

int main(int argc, char *argv[])
{
int nRet;

nRet = RemoveSameStringLine(argc, argv);
printf("RemoveSameStringLine-ret:%d\n", nRet);

return 0;
}

周靖峰 2010-05-20
  • 打赏
  • 举报
回复
不懂,等待高手解答
gary_cai 2010-05-20
  • 打赏
  • 举报
回复
有容器转然后删除指定那行再遍历容器重写到另外一个文件中
bobo364 2010-05-20
  • 打赏
  • 举报
回复
#include<stdio.h>

int read(char *fn,char a[][100],int maxLine)
{
FILE *f=fopen(fn,"r");
int i=0;
if(f)
{
while(!feof(f))
{
fgets(a[i],100,f);
if(++i>=maxLine)
{
break;
}
}
}
fclose(f);
return i;
}
char a[500][100];
void main()
{
int i=0;
int n=read("c:\\service.log",a,500);
for(i=0;i<n;i++)
{
printf("%s",a[i]);
}

}
bobo364 2010-05-20
  • 打赏
  • 举报
回复
或者另一个思路是,将所有的东西复制到一个字符数组中,包括换行符,08的话就用c的字符串匹配函数匹配,有的话,就删除数组中的一行,请参照如下代码fgets读取文件中大小固定字符到一个数组中

#include<stdio.h>

int read(char *fn,char a[][100],int maxLine)
{
FILE *f=fopen(fn,"r");
int i=0;
if(f)
{
while(!feof(f))
{
fgets(a[i],100,f);
if(++i>=maxLine)
{
break;
}
}
}
fclose(f);
return i;
}
char a[500][100];
void main()
{
int i=0;
int n=read("c:\\service.log",a,500);
for(i=0;i<n;i++)
{
printf("%s",a[i]);
}

}
把read的第一个参数改成你的文件名。
ohaiyou 2010-05-20
  • 打赏
  • 举报
回复
╮(╯▽╰)╭ 其实我的题目是 根据 找到字符串 03 然后删除字符串所在的那一行的 意思
liboyanghui 2010-05-20
  • 打赏
  • 举报
回复
去不读出来,存储到cstring strContent中
CString strPreEnter[3];//
for (i = 0; i != 3; i++)
{
pos = strContent_temp.Find("\r\n");//遍历前三行
strPreEnter[i] = strContent_temp.Left(pos);
strContent_temp = strContent_temp.Right(strContent_temp.GetLength()-pos-2)
}

删掉后,再存储。嘿嘿,法子有点笨!
lylm 2010-05-20
  • 打赏
  • 举报
回复
int i,k,L=3; //改成L=8不就行了
bobo364 2010-05-20
  • 打赏
  • 举报
回复
但关于fsetpos,fgetpos的用法还是有点不明白

fgetpos
语法:
#include <stdio.h>
int fgetpos( FILE *stream, fpos_t *position );



fgetpos()函数保存给出的文件流(stream)的位置指针到给出的位置变量(position)中. position变量是fpos_t类型的(它在stdio.h中定义)并且是可以控制在FILE中每个可能的位置对象. fgetpos()执行成功时返回0,失败时返回一个非零值.

它返回的position我不明白,请高手解答
ohaiyou 2010-05-20
  • 打赏
  • 举报
回复
这个第三行不是确定 如果03 变成 08 了呢 怎么删除ccccccc行啊?
bobo364 2010-05-20
  • 打赏
  • 举报
回复
貌似这个可以实现你的要求

删除文件里的一行内容,后面的行向前移动一行,清空最后一行,程序如下:

假定一行不超过1000字符,
删去第3行,L=3.
输入输出文件名 a.txt


#include<stdio.h> 

void main()
{
FILE *fin;
fpos_t pos_w,pos_r,pos;
int i,k,L=3;
char *one_line;

one_line = (char *) malloc(1000*sizeof(char));

fin = fopen ("a.txt","rb+");
for (i=1;i<L;i++) fgets(one_line,999,fin);

fgetpos (fin,&pos_w);
fgets(one_line,999,fin); // delete
fgetpos (fin,&pos_r);
pos = pos_r;

while (1 == 1)
{
fsetpos (fin,&pos);
if (fgets(one_line,999,fin) ==NULL) break;
fgetpos (fin,&pos_r);
pos = pos_w;
fsetpos (fin,&pos);
fprintf(fin,"%s",one_line);
fgetpos (fin,&pos_w);
pos = pos_r;
}
pos = pos_w;
fsetpos (fin,&pos);
k = strlen(one_line);
for (i=0;i<k;i++) fputc(0x20,fin);
fclose(fin);
}
加载更多回复(1)

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧