//==============================================================================
// Filename : zzl.c
// Description :
//
// Author : RollStone (rs), jealdean@outlook.com
// Created : October 22, 2014, 5:15 PM
// Copyright : 2007-2014 RollStone. All Rights Reserved.
// Last_Change : October 22, 2014, 5:15 PM
// Version : 1.0
//==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "zzl.h"
#define PIN //param in
#define POUT //param out
#define PINOUT //prarm both in and out
#define OPEN_MODE_READONLY "rt"
#define WORD_DELIM " ,';:.\n\r\""
/**
* pick up word in buf
* @param buf
* @param pos
* @return num of words
*/
int pick_words(PIN char *buf,POUT char **pResultArray);
int main(int argc, char **argv) {
int fileSize, n;
char *fileBuf;
char *p, *q;
FILE *fp = fopen(argv[1], OPEN_MODE_READONLY);
if (!fp) {
perror("fopen");
return -1;
}
//get file size of bytes
fseek(fp, 0L, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0L, SEEK_SET); // rewind the fp to start position of file
fileBuf = (char*) malloc(sizeof (char)*(fileSize + 1));
fread((void*) fileBuf, sizeof (char), fileSize, fp);
if (fp) {
fclose(fp);
fp = 0;
}
char **resultArray = (char**) calloc(sizeof (char*), fileSize / 2);
n = pick_words(fileBuf, resultArray);
printf("there has %d words. they are :\n", n);
char **pp=resultArray;
while(*pp){
printf("%s\n",*pp);
pp++;
}
if (fileBuf) {
free(fileBuf);
fileBuf = 0;
}
if (resultArray) {
free(resultArray);
resultArray = 0;
}
return 0;
}
int pick_words(PIN char *wordbuf, POUT char **pResultArray) {
char *cur;
int wordLength = 0;
cur = strtok(wordbuf, WORD_DELIM);
if (cur != 0) {
wordLength++;
*pResultArray = cur;
pResultArray++;
do {
cur = strtok(NULL, WORD_DELIM);
wordLength++;
*pResultArray = cur;
pResultArray++;
} while (cur);
}
return wordLength;
}
挑选出英文字符串中的 单词 并给出单词个数(C语言实现版本,C++版待完成)
最新推荐文章于 2023-01-08 23:03:34 发布