简体   繁体   English

计算C中的行,数字和字符

[英]Counting lines, numbers, and characters in C

I'm new to C and I got an assignment today that requires that I read text in from a file, count the number of lines, characters, and words, and return it in a specific format. 我是C语言的新手,今天我收到一份作业,要求我从文件中读取文本,计算行数,字符和单词的数量,并以特定格式返回它。

Just to be clear - I need to read in this text file: 为了清楚起见,我需要阅读以下文本文件:

"I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past I will turn the inner eye to see its path.
Where the fear has gone there will be nothing... only I will remain"
  Litany Against Fear, Dune by Frank Herbert

and have it output like so: 并使其输出如下:

1)"I must not fear.[4,17]
2)Fear is the mind-killer.[4,24]
3)Fear is the little-death that brings total obliteration.[8,56]
4)I will face my fear.[5,20]
5)I will permit it to pass over me and through me.[11,48]
6)And when it has gone past I will turn the inner eye to see its path.[16,68]
7)Where the fear has gone there will be nothing... only I will remain"[13,68]
8)      Litany Against Fear, Dune by Frank Herbert[7,48]

Now, I've written something that will accept the file, it counts the number of lines properly, but I have 2 major issues - 1. How do I get the text from the file to appear in the output? 现在,我写了一些可以接受文件的东西,它可以正确地计算行数,但是有两个主要问题-1.如何从文件中获取文本以显示在输出中? I can't get that at all. 我根本无法理解。 My word count doesn't work at all, and my character count is off too. 我的字数完全不起作用,我的字符数也没有。 Can you please help? 你能帮忙吗?

#include <stdio.h>

#define IN 1
#define OUT 0

void main()
{
    int numChars = 0;
    int numWords = 0;
    int numLines = 0;
    int state = 0;
    int test = 0;

    FILE *doesthiswork;

    doesthiswork = fopen("testWords.in", "r");
    state = OUT;

    while ((test = fgetc(doesthiswork)) != EOF) 
    {
        ++numChars;
        if ( test == '\n') 
        {
            ++numLines;
            if (test == ' ' || test == '\t' || test == '\n')
            {      
                state = OUT;
            } 
            else if (state == OUT)
            {
                state = IN;
                ++numWords;
            }

         }
         printf("%d) I NEED TEXT HERE. [%d %d]\n",numLines, numWords, numChars);
     }

}
  1. It will be better if you use getline() function to read each line from the file. 如果使用getline()函数从文件中读取每一行会更好。

  2. And after reading the line process it using strtok() function. 在读取了行处理之后,使用strtok()函数。 With this you will get the number of words in the line and save it in a variable. 这样,您将获得该行中的单词数并将其保存在变量中。

  3. Then process each variable and get the number of characters. 然后处理每个变量并获得字符数。

  4. Output the line number, number of words and the number of characters. 输出行号,单词数和字符数。

Then read another line and so on. 然后读取另一行,依此类推。

  1. How do I get the text from the file to appear in the output? 如何从文件中获取文本以显示在输出中?
    It should be stored there by preparing a buffer. 应该通过准备缓冲区将其存储在此处。

  2. My word count doesn't work at all, and my character count is off too. 我的字数完全不起作用,我的字符数也没有。
    Order in which the test is wrong. 测试错误的顺序。

fix like this: 像这样修复:

#include <stdio.h>

#define IN  1
#define OUT 0

int main(){
    int numChars = 0;
    int numWords = 0;
    int numLines = 0;
    int state = OUT;
    int test;
    char buffer[1024];
    int buff_pos = 0;
    FILE *doesthiswork;

    doesthiswork = fopen("data.txt", "r");
    state = OUT;

    while((test = fgetc(doesthiswork)) != EOF) {
        ++numChars;
        buffer[buff_pos++] = test;
        if(test == ' ' || test == '\t' || test == '\n'){
            state = OUT;
            if(test == '\n') {
                ++numLines;
                --numChars;//no count newline
                buffer[--buff_pos] = '\0';//rewrite newline
                printf("%d)%s[%d,%d]\n", numLines, buffer, numWords, numChars);
                buff_pos = 0;
                numWords = numChars = 0;
            }
         } else {
            if(state == OUT){
                state = IN;
                ++numWords;
            }
         }
    }
    fclose(doesthiswork);
    if(buff_pos != 0){//Input remains in the buffer.
        ++numLines;
        buffer[buff_pos] = '\0';
        printf("%d)%s[%d,%d]\n", numLines, buffer, numWords, numChars);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM