C语言获取文件中单词并进行处理
2012-06-16 11:43
369 查看
/****************************************************** * * follows begin the word get process * ******************************************************/ bool isWord(char c) { if ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')) || ('-' == c)) { return true; } return false; } struct word_geter_engine { char * buffer; /* contain the words */ char * current; /* where we reach in the buffer */ char * end; /* the last position + 1 */ int bufferSize; /* the capacity of the buffer */ char wordBuf[WORDLEN]; /* store the word have got */ bool needFill; /* indicate whether need fill the buffer */ }; bool initStorer(int capacity, struct word_geter_engine * engine) { engine->buffer = malloc(capacity); if (NULL == engine->buffer) { return false; } engine->current = engine->buffer; engine->end = engine->buffer + capacity; engine->bufferSize = capacity; engine->needFill = true; return true; } bool fillEngine(FILE * to_read, struct word_geter_engine * engine) { int leftLen = 0; int read_byte = 0; char * start = NULL; if (engine->current != engine->buffer) { leftLen = engine->end - engine->current; memcpy(engine->buffer, engine->current, leftLen); engine->current = engine->buffer; } start = engine->buffer + leftLen; leftLen = engine->bufferSize - leftLen; read_byte = fread(start, 1, leftLen, to_read); if (0 == read_byte) { return false; } engine->end = start + read_byte; engine->needFill = false; return true; } bool getWord(struct word_geter_engine * engine) { char * p = engine->current; int wordlen = 0; engine->wordBuf[0] = ''; while (!isWord(*p) && (p < engine->end)) { p++; } engine->current = p; if (engine->end == p) { engine->needFill = true; return false; } while (isWord(*p) && (p < engine->end)) { p++; } if (engine->end == p) { engine->needFill = true; return false; } wordlen = p - engine->current; if (wordlen > (sizeof(engine->wordBuf) - 1)) { wordlen = sizeof(engine->wordBuf) - 1; } memcpy(engine->wordBuf, engine->current, wordlen); engine->wordBuf[wordlen] = ''; engine->current = p; return true; } /* * function process read word from the to_read and call the processer * use the word, you should give the processer. */ bool process(FILE * to_read, bool (*processer)(const char * str)) { struct word_geter_engine engine; if (!initStorer(BUFLEN, &engine)) { return false; } while (engine.needFill && !feof(to_read)) { fillEngine(to_read, &engine); while (!engine.needFill) { if (!getWord(&engine)) { continue; } processer(engine.wordBuf); } } return true; } /*********************** End process ***********************/
process函数是读取函数,对读取到的单词进行处理可以通过processer回调函数进行处理。
这个代码是自己做hash函数测试的时候写的,为了获取大量的字符串。
可以通过调整isWord函数对要获取有哪些字符组成的单词进行控制
相关文章推荐
- 用C语言对txt文件进行读写处理的过程
- STL---获取某个文件夹下的所有txt文件,并进行处理
- FPGA工程中用C语言对文件进行处理_生成mif文件
- 利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
- C语言中时间的处理(二、获取当前的时间,秒,毫秒,纳秒)
- C语言stat()函数:获取文件状态
- 通过SiteMapDataSource动态获取SiteMap文件进行权限设置
- 【C语言】getchar单个字符依次输入(可对单个字符进行处理)
- 详解keil采用C语言模块化编程时全局变量、结构体的定义、声明以及头文件包含的处理方法
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 【原创】C语言处理文件数据(2)
- 117 用C语言对数据或文件内容进行加密
- Mybatis --- 映射文件、参数处理、参数值的获取、select元素
- 用shell脚本配合awk文本处理工具进行文件处理
- 如何使用 类进行文件的 I/O 处理
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- C# 获取文件根据创建时间进行排序
- C语言获取文件CRC32算法附加CRC原理(转)
- perl,读取所需文件的路径,然后打开相应的文件,并对文件中的DNA序列进行计数,substr函数对长字符串的片段化处理功能
- 详解keil采用C语言模块化编程时全局变量、结构体的定义、声明以及头文件包含的处理方法!