代码分享:C I/O操作和字符查寻,TR1 C++正则多行查找

代码分享:C I/O操作和字符查找,TR1 C++正则多行查找


C/C++ code

//    功能:输出一个文件的最后n行数据,如果文件总行数不足n行,则显示全部数据
//    基本思路:把文件读入缓存,然后查找倒数第10个换行符
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 反查找Buf缓冲区中字符ch, 次数num次, 如果找不到返回NULL,少于num,返回Buf
const char* rfindBuf_chrTimes(char* Buf, size_t BufSize , int ch, size_t num);

int main(int argc, char* argv[])
{
  const char* fileName = "main.cpp"; //argv[1];
  FILE* pFile = fopen(fileName, "r");
  if (pFile == NULL) {
    perror("打开文件时错误!");
    return -1;
  }
  // 获得文件大小
  fseek(pFile, 0, SEEK_END);
  long fileSize = ftell(pFile);
  fseek(pFile, 0, SEEK_SET);

  // 把文件读到缓冲区
  char* fileBuf = new char[fileSize + 1];
  memset(fileBuf,0, fileSize + 1);
  fread(fileBuf, fileSize, 1, pFile);

  const char* ps = NULL;
  ps = rfindBuf_chrTimes(fileBuf, fileSize , '\n', 10);
  // 测试代码
  printf("%s:文件大小%ld\n", fileName,fileSize);
  printf("%s\n", ps);
  delete[] fileBuf;
  fclose(pFile);
  return 0;
}
// 反查找Buf缓冲区中字符ch, 次数num次, 如果找不到返回NULL,少于num,返回Buf
const char* rfindBuf_chrTimes(char* Buf, size_t BufSize , int ch, size_t num)
{
  const char* ret = Buf;
  const char* ps = Buf + BufSize;
  size_t cnt = 0;
  while (ps > Buf) {
    if (*ps == ch)
      cnt++;
    if (cnt == num)
      return (ret = ++ps);
    ps--;
  }
  if (cnt == 0)
    return NULL;

  return ret;
} // 有些编辑器,会在文件最后一行插入一个空行,可能输出看上去是9行



------解决方案--------------------
不错,更新挺快~~
------解决方案--------------------
(好)
------解决方案--------------------
强大。。。
------解决方案--------------------
谢谢楼主分享
保存了以后看
------解决方案--------------------
目前还不看懂,留以后研究,谢谢LZ
------解决方案--------------------

------解决方案--------------------
感谢分享
------解决方案--------------------
收藏了
------解决方案--------------------
学习了
------解决方案--------------------
不懂C的文件操作
------解决方案--------------------
推崇楼主的分享精神!
转《代码之美》:
C/C++ code
//摘自《代码之美》
// 字符     含义
// .        匹配任意的单个字符
// ^        匹配输入字符串的开头
// $        匹配输入字符串的结尾
// *        匹配前一个字符的零个或者多个出现
#include <stdio.h>
int matchhere(char *regexp, char *text);

int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text
   do {// a * matches zero or more instances
       if (matchhere(regexp, text)) return 1;
   } while (*text != '\0' && (*text++ == c || c == '.'));
   return 0;
}
int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text
   if (regexp[0] == '\0') return 1;
   if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);
   if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';
   if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);
   return 0;
}

int match(char *regexp, char *text) {// match: search for regexp anywhere in text
    if (regexp[0] == '^') return matchhere(regexp+1, text);
    do {// must look even if string is empty
        if (matchhere(regexp, text)) return 1;
    } while (*text++ != '\0');
    return 0;
}
void main() {
    printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));
    printf("%d==match(^a  ,abc)\n",match("^a"  ,"abc"));
    printf("%d==match(c$  ,abc)\n",match("c$"  ,"abc"));
    printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));
    printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));
    printf("-------------------\n");
    printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));
    printf("%d==match(^B  ,abc)\n",match("^B"  ,"abc"));
    printf("%d==match(A$  ,abc)\n",match("A$"  ,"abc"));
    printf("%d==match(a..c,abc)\n",match("a..c","abc"));
    printf("%d==match(a.*d,abc)\n",match("a.*d","abc"));
}
//1==match(abc ,abc)
//1==match(^a  ,abc)
//1==match(c$  ,abc)
//1==match(a.c ,abc)
//1==match(a.*c,abc)
//-------------------
//0==match(ABC ,abc)
//0==match(^B  ,abc)
//0==match(A$  ,abc)
//0==match(a..c,abc)
//0==match(a.*d,abc)