C重置FOR循环中的数据计数器

C重置FOR循环中的数据计数器,c,loops,for-loop,counter,reset,C,Loops,For Loop,Counter,Reset,我有一个非常大的文本文件,我正试图对它进行文字分析。在字数统计中,我可能也在寻找其他信息,但为了简单起见,我省略了这一点。 在这个文本文件中,我有用星号“*”分隔的文本块。下面的代码扫描文本文件并打印出字符和单词,但我希望在遇到星号后重置计数器,并将所有信息存储在某种表格中。我不太担心如何制作表格,因为我不确定如何在星号之间为每个文本块循环相同的计数代码 也许是像这样的循环 for (arr = strstr(arr, "*"); arr; arr = strstr(arr + strlen("

我有一个非常大的文本文件,我正试图对它进行文字分析。在字数统计中,我可能也在寻找其他信息,但为了简单起见,我省略了这一点。 在这个文本文件中,我有用星号“*”分隔的文本块。下面的代码扫描文本文件并打印出字符和单词,但我希望在遇到星号后重置计数器,并将所有信息存储在某种表格中。我不太担心如何制作表格,因为我不确定如何在星号之间为每个文本块循环相同的计数代码

也许是像这样的循环

for (arr = strstr(arr, "*"); arr; arr = strstr(arr + strlen("*"), "*"))  
示例文本文件:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I have a sentence. I have two sentences now.
*
I have another sentence. And another.
*
I'd like to count the amount of words and characters from the asterisk above this 
one until the next asterkisk, not including the count from the last one.
*
...
    ...
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    (EOF)

Desired output:

    *#      #words     #alphaChar
    ----------------------------
    1        9           34  
    -----------------------------
    2        5           30
    -----------------------------
    3       28           124
    ...
    ...


I have tried

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        int main()
          {
          int characterCount=0;
          int counterPosition, wordCount=0, alphaCount=0;

          //input file
          FILE *file= fopen("test.txt", "r");
          if (file== NULL)
            printf("Cannot find the file.\n");


          //Count total number of characters in file
          while (1)
              {
              counterPosition = fgetc(speechFile);
              if (counterPosition == EOF)
                break;
              ++characterCount;
              }

          rewind(file); // Sends the pointer to the beginning of the file

          //Dynamically allocate since array size cant be variable
          char *arr= ( char*) malloc(totalCharacterCount);

          while(fscanf(speechFile, "%c", &arr[i]) != EOF ) //Scan until the end of file.
            i++;   //increment, storing each character in a unique position



              for(i = 0; i <characterCount; i++)
                  {
                  if(arr[i] == ' ') //count words
                    wordCount++;

                  if(isalpha(arr[i]))  //count letters only
                    alphaCount++;

                  }//end for loop

              printf("word count is %d and alpha count is %d", wordCount,alphaCount);
          }
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
我有一句话。我现在有两句话。
*
我还有一句话。还有一个。
*
我想从上面的星号中数一数单词和字符的数量
一个直到下一个星号,不包括上一个的计数。
*
...
...
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
(EOF)
期望输出:
*##单词#字母字符
----------------------------
1        9           34  
-----------------------------
2        5           30
-----------------------------
3       28           124
...
...
我试过了
#包括
#包括
#包括
int main()
{
int characterCount=0;
整数对位,字数=0,字母数=0;
//输入文件
FILE*FILE=fopen(“test.txt”、“r”);
if(file==NULL)
printf(“找不到文件。\n”);
//计算文件中的字符总数
而(1)
{
对位=fgetc(演讲文件);
if(对位==EOF)
打破
++字符数;
}
倒带(文件);//将指针发送到文件的开头
//动态分配,因为数组大小不能可变
char*arr=(char*)malloc(totalCharacterCount);
while(fscanf(speechFile,“%c”,&arr[i])!=EOF)//扫描到文件末尾。
i++;//递增,将每个字符存储在唯一的位置

对于(i=0;i由于数组arr[]中包含完整的文本文件,因此需要使用
*
作为分隔符来分割该字符串
arr
。可以使用
strtok()
使用
*
作为分隔符分割该字符串。然后对每个标记执行字数计数和字符计数操作。阅读此链接了解相关信息。

字数计数和字母计数未初始化。此外,还可以在每个空格上递增字数计数,因此“”(2个空格)将计为两个单词。和
char*arr=(char*)malloc(totalCharacterCount*sizeof(int));
可以是
char*arr=malloc(totalCharacterCount);
添加了修复程序。但这不是问题。我重新编写了大部分代码,而不是复制和粘贴,所以我忘了为什么程序要进行三次(文件两次,数组一次)当只需要一次传球时。另外:该计划似乎与你的目标几乎没有关系。你说的三次传球是什么意思?我对这一点不熟悉。此外,该计划与我的目标非常相关。我将“抽签”图表我自己。我只是问如何在每组星号之间重置计数器。我记得有人在另一篇帖子中提到了strtok,但出于某种原因,我认为它是无关紧要的。你说的有道理,我会试试看。谢谢!@user3465668好的。有一些问题正在讨论strtok()的使用在stackoverflow中,所以我无法添加代码。请阅读其中一个。检查以了解如何使用strtok。它可能会帮助您编写代码