words counting in file like linux wc command in C

后端 未结 2 392
不知归路
不知归路 2020-12-22 03:14

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 w

相关标签:
2条回答
  • 2020-12-22 03:51

    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.

    0 讨论(0)
  • 2020-12-22 03:54

    use isspace(char ch) function to check whitespaces.

    int isInWord = 0;/*false*/
    while(read(file,buffer,1)==1){
        bytes++ ;
        if(!isspace(buffer[0])){
             isInWord = 1;/*true*/
             continue;
        }else{
          if(buffer[0] == '\n'){
            newLine++;
          }else{
            if(isInWord)
             words++;
          }
          isInWord = 0;
       }
    }
    
    0 讨论(0)
提交回复
热议问题