首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

linux上C语言实现统计文本文件的操作

2012-09-04 
linux下C语言实现统计文本文件的操作编写一个程序,分析一个文本文件中,某一个指定文本字符串的出现频率,出

linux下C语言实现统计文本文件的操作
编写一个程序,分析一个文本文件中,某一个指定文本字符串的
出现频率,出现行号,和出现次数。
限定:文本文件里,都是ascii码。 指定的文本字符串,最多32字节。

[解决办法]
fopen 打开文件
fgets 逐行读取
[解决办法]
读取一行以后使用strtok分割字符串
[解决办法]
给个例子:同级文件中"0.2"字符串出现次数。

C/C++ code
#include <stdio.h>  #include <stdlib.h>  #include <string.h>  int main()  {      char* dest = "0.2";  //要统计的字串    int count = 0;    char line[100];      FILE * pFile;      int irow = 0;      int icol = 0;      int colNum = 0;      pFile = fopen ("input.txt","r");      if (pFile!=NULL)      {          while(1)          {              if (fgets (line, 100, pFile)==NULL)                  break;              // 分割每行              char* fliter = " ";              char* pch = strtok (line, fliter);              while (pch != NULL)              {                  if (strcmp(pch, dest)==0)                    count++;                // 继续分割                  pch = strtok (NULL, fliter);              }              ++irow;              colNum = icol;              icol = 0;          }          fclose (pFile);      }      printf("%d\n", count);    getchar();  }  //input.txt  //0.1 0.2 0.3  //0.4 0.2 0.6  //0.7 0.8 0.9 

热点排行