请问一个从文件搜索关键字的问题?

chp845 2012-10-19 02:00:06
我想从一个文件指定位置开始搜索一对关键字: "stream" 和 "endstream";
文件打开是使用"rb"打开,文件中有乱码存在.
1.一次把文件加载进内存,在读取,但是这样文件有时候很大,会很占用资源
2.移动fseek到指定位置,用freed字节来判断,但是这样频繁读取磁盘,速度慢?
3.缓冲一个指定大小字节,然后,再中其中查找关键字,查找不到读取下个缓冲

请问有没有其他更好的方法可以做到,资源消耗合理以及速度比较的?
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-10-19
  • 打赏
  • 举报
回复
仅供参考
#include <sys\stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_CLU_BYTES 65536
FILE *fo;
int fh;
__int64 offs,offs1;
__int64 rvi64;
int rv,wrv;
unsigned char buf[MAX_CLU_BYTES];
char ofn[_MAX_PATH];
char offstr[80];
void strcpybutcomma(char *t,char *s) {
char c;

while (1) {
c = *s++;
if (','!=c) *t++ = c;
if (0==c) break;
}
}
void main(int argc,char **argv) {
if (argc<3) {
printf("Copy File Tail.\n");
printf("Usage:\n");
printf(" cft filename.ext offset_begin[-offset_end]\n");
printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");
printf("Note: Byte at offset_end is NOT included.\n");
printf("Example:\n");
printf(" cft abc.rar 12345\n");
printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");
printf(" cft abc.rar 123-12345\n");
printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");
printf(" cft abc.rar 0xAB-0xCD\n");
printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");
return;
}
strcpybutcomma(offstr,argv[2]);
rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);
if (rv==0) {
printf("offset %s is not number\n",argv[2]);
return;
}
fh=_sopen(argv[1],_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYWR);
if (fh==-1) {
printf("_sopen %s errno=%d\n",argv[1],errno);
return;
}
if (rv==1) {
offs1=_filelengthi64(fh);
if (offs1==-1i64) {
printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);
_close(fh);
return;
}
} else {//rv==2
if (offs1<offs) {
printf("%s offset_begin>offset_end error\n",argv[2]);
_close(fh);
return;
}
}
rvi64=_lseeki64(fh,offs,SEEK_SET);
if (rvi64!=offs) {
printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);
_close(fh);
return;
}
sprintf(ofn,"%s-",offstr);
strcat(ofn,argv[1]);
fo=fopen(ofn,"wb");
if (fo==NULL) {
_close(fh);
printf("fopen %s error\n",ofn);
return;
}
cprintf("\n%I64u\r",offs);
while (1) {
rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));
if (rv==0) break;//
if (rv<0) {
fclose(fo);
_close(fh);
printf("_read %s offset %I64u error\n",argv[1],offs);
return;
}
wrv=fwrite(buf,1,rv,fo);
if (wrv!=rv) {
fclose(fo);
_close(fh);
printf("fwrite %s error\n",ofn);
return;
} else {
offs+=rv;
cprintf("%I64u\r",offs);
if (offs>=offs1) break;//
}
}
fclose(fo);
_close(fh);
printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn);
}
chp845 2012-10-19
  • 打赏
  • 举报
回复
有人可以知道下吗?
chp845 2012-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

加载映射,使用windows api去加载,然后如果可以的话就一行一行的读 ,
[/Quote]
windows api可也用,我部分代码需要兼容linux运行环境!
一行行读,其中有乱码存在!
chp845 2012-10-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

如果是文本文件,可以一行一行的读。。。
[/Quote]
不是文本文件,里面有乱码?所以按行读不太可靠!
zslInSz 2012-10-19
  • 打赏
  • 举报
回复
加载映射,使用windows api去加载,然后如果可以的话就一行一行的读 ,
mymtom 2012-10-19
  • 打赏
  • 举报
回复
如果是文本文件,可以一行一行的读。。。

69,369

社区成员

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

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