全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2234
推到 Plurk!
推到 Facebook!

在記憶體中尋找資料

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-11 18:13:30 IP:61.218.xxx.xxx 未訂閱

在記憶體中尋找資料

在記憶體中尋找資料,有個ANSI C內建函式叫strstr(),可以在字串中尋找字串,這裡提供一個memmem(),可以在陣列中尋找指定的資料.
/*    Date last modified: 05-Jul-1997 */    /*
**  MEMMEM.C - A strstr() work-alike for non-text buffers
**
**  public domain by Bob Stout
**
**  Warning: The memchr() in Borland C/C   versions *prior* to 4.x is broken!
*/    #include < string.h >
#include "snip_str.h"    #if defined(__cplusplus) && __cplusplus
 extern "C" {
#endif    void *memmem(const void *buf, const void *pattern, size_t buflen, size_t len)
{
#if defined(__TURBOC__) && (__TURBOC__ >= 0x500)          char *bf = (char *)buf, *pt = (char *)pattern, *p = bf;          while (len <= (buflen - (p - bf)))
      {
            if (NULL != (p = memchr(p, (int)(*pt), buflen - (p - bf))))
            {
                  if (Success_ == memcmp(p, pattern, len))
                        return p;
                  else    p;
            }
            else  break;
      }
      return NULL;    #else /* Borland/Turbo C/C   version prior to 4.x */          size_t i, j;
      char *bf = (char *)buf, *pt = (char *)pattern;          if (len > buflen)
            return (void *)NULL;          for (i = 0; i <= (buflen - len);   i)
      {
            for (j = 0; j < len;   j)
            {
                  if (pt[j] != bf[i   j])
                        break;
            }
            if (j == len)
                  return (bf   i);
      }
      return NULL;    #endif
}    #if defined(__cplusplus) && __cplusplus
 }
#endif    #ifdef TEST    #include < stdio.h >    main()
{
      char buf[13] = "\0""12344567890\x1b";
      char a[3] = "456";
      char b[3] = "\0""12";
      char c[3] = "90\x1b";
      char d[3] = "ABC";
      char e[3] = "0\x1b""\0";
      char f[1] = "\x1b";
      char *ptr;          if (NULL == (ptr = memmem(buf, a, 13, 3)))
            puts("a not found in buf");
      else  printf("a found in buf at posn %d\n", ptr - buf);          if (NULL == (ptr = memmem(buf, b, 13, 3)))
            puts("b not found in buf");
      else  printf("b found in buf at posn %d\n", ptr - buf);          if (NULL == (ptr = memmem(buf, c, 13, 3)))
            puts("c not found in buf");
      else  printf("c found in buf at posn %d\n", ptr - buf);          if (NULL == (ptr = memmem(buf, d, 13, 3)))
            puts("d not found in buf");
      else  printf("d found in buf at posn %d\n", ptr - buf);          if (NULL == (ptr = memmem(buf, e, 13, 3)))
            puts("e not found in buf");
      else  printf("e found in buf at posn %d\n", ptr - buf);          if (NULL == (ptr = memmem(buf, f, 13, 1)))
            puts("f not found in buf");
      else  printf("f found in buf at posn %d\n", ptr - buf);
      
      return 0;
}    #endif /* TEST */
聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2002/12/11 19:20:34
系統時間:2024-11-21 21:06:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!