- 论坛徽章:
- 0
|
本帖最后由 free4machine 于 2010-05-31 08:36 编辑
我想对 一个 文件 逐词 读,再做对比, 比如我有以下文件,
cat /tmp/tlf.txt
85 77 17 74 82 26 14
想和
33 22 11 55 82 22 10
对比, 看下有多少 相同的, 也就是 彩票 中奖对比。
程序:- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char *buf;
- char *token;
- int c;
- char *space=" ";
- char str2[1024];
- char *file="/tmp/tlf.txt";
- FILE *fp;
- if (( fp = fopen(file,"r")) == NULL) {
- perror("fopen:");
- }
- fgets(str2,1024,fp);
- printf("str is: %s\n",str2);
- buf = str2;
- while((token = strsep(&buf, " ")) != NULL){
- if(strcmp(token,space)){
- c++;
- printf("%s\n", token);
- }
- }
- printf("total : %d\n",c);
- return 0;
- }
复制代码 输出:- str is: 85 77 17 74 82 26 14
- 85
- 77
- 17
- 74
- 82
- 26
- 14
- total : 29
复制代码 奇怪了, 不是才 7 个数, 怎么 是 29 个了?
我尝试 把改成, 输出:- str is: 85 77 17 74 82 26 14
- |||85||||77||||17||||74||||82||||26||||14||total : 29
复制代码 我就更看不懂了, 难道不应该是这个吗? : |
|