也就是说用C文件计数如Linux wc命令 [英] words counting in file like linux wc command in C

查看:213
本文介绍了也就是说用C文件计数如Linux wc命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一些东西,就像Linux命令厕所来算的话,新的生产线和字节中的任何类型的文件,我可以只使用读取C函数。我写了这个code和我得到了新行和字节的正确值,但我不计的话得到正确的值。

 字节INT = 0;
INT字= 0;
INT NEWLINE = 0;
炭缓冲液[1];
INT文件=打开(MYFILE,O_RDONLY);
如果(文件== -1){
  的printf(找不到:%S \\ n,MYFILE);
}
其他{
  最后烧焦='C';
  而(读(文件,缓冲液,1)== 1){
    字节++;
    如果(缓冲[0] ==''和;&安培;最后=!''和;&安培;最后='\\ n'!){
      也就是说++;
    }
    否则,如果(缓冲[0] =='\\ n'){
      NEWLINE ++;
      如果(最后='!'和;&安培;最后!='\\ n'){
        也就是说++;
      }
    }
    最后=缓冲器[0];
  }
  的printf(%d个%D%S \\ n,换行,字数,字节数,MYFILE);
}


解决方案

您应该反转的逻辑。而不是寻找一个空间,并增加你的字数,寻找一个非空间来增加字数。此外,它可以帮助使用状态变量与看最后一个字符:

  INT主要(无效)
{
   为const char * MYFILE =的test.txt;
   INT字节= 0;
   INT字= 0;
   INT NEWLINE = 0;
   炭缓冲液[1];
   INT文件=打开(MYFILE,O_RDONLY);
   枚举状态{WHITESPACE,WORD};
   INT状态= WHITESPACE;
   如果(文件== -1){
      的printf(找不到:%S \\ n,MYFILE);
   }
   其他{
      焦炭最后='';
      而(读(文件,缓冲液,1)== 1)
      {
         字节++;
         如果(缓冲[0] ==''||缓冲区[0] =='\\ t')
         {
            状态= WHITESPACE;
         }
         否则,如果(缓冲[0] =='\\ n')
         {
            NEWLINE ++;
            状态= WHITESPACE;
         }
         其他
         {
            如果(状态== WHITESPACE)
            {
               也就是说++;
            }
            状态= WORD;
         }
         最后=缓冲器[0];
      }
      的printf(%d个%D%S \\ n,换行,字数,字节数,MYFILE);
   }}

看来,厕所有一定的逻辑相对于标点符号不是的话,那这个code不处理。

I am trying to write something that works like the Linux command wc to count words, new lines and bytes in any kind of files and i can only use the C function read. I have written this code and i am getting the correct values for newlines and bytes but i am not getting the correct value for counted words.

int bytes = 0;
int words = 0;
int newLine = 0;
char buffer[1];
int file = open(myfile,O_RDONLY);
if(file == -1){
  printf("can not find :%s\n",myfile);
}
else{
  char last = 'c'; 
  while(read(file,buffer,1)==1){
    bytes++;
    if(buffer[0]==' ' && last!=' ' && last!='\n'){
      words++;
    }
    else if(buffer[0]=='\n'){
      newLine++;
      if(last!=' ' && last!='\n'){
        words++;
      }
    }
    last = buffer[0];
  }        
  printf("%d %d %d %s\n",newLine,words,bytes,myfile);        
} 

解决方案

You should reverse your logic. Rather than look for a space, and increment your word count, look for a non-space to increment the word count. Also, it can help to use a state variable versus looking at the last char:

int main(void)
{
   const char *myfile = "test.txt";
   int bytes = 0;
   int words = 0;
   int newLine = 0;
   char buffer[1];
   int file = open(myfile,O_RDONLY);
   enum states { WHITESPACE, WORD };
   int state = WHITESPACE;
   if(file == -1){
      printf("can not find :%s\n",myfile);
   }
   else{
      char last = ' '; 
      while (read(file,buffer,1) ==1 )
      {
         bytes++;
         if ( buffer[0]== ' ' || buffer[0] == '\t'  )
         {
            state = WHITESPACE;
         }
         else if (buffer[0]=='\n')
         {
            newLine++;
            state = WHITESPACE;
         }
         else 
         {
            if ( state == WHITESPACE )
            {
               words++;
            }
            state = WORD;
         }
         last = buffer[0];
      }        
      printf("%d %d %d %s\n",newLine,words,bytes,myfile);        
   } 

}

It appears that wc has some logic with respect to punctuation characters not being words, that this code does not handle.

这篇关于也就是说用C文件计数如Linux wc命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆