【问题标题】:Searching for a keyword in a txt file and logging it with C [closed]在 txt 文件中搜索关键字并用 C 记录它 [关闭]
【发布时间】:2019-02-25 01:05:49
【问题描述】:

我正在尝试使用 C 来搜索包含 C 代码的文件。它旨在搜索整个文件,找到某些关键字或字符(例如查找 Ints、Longs、For 循环等)并通过递增计数器记录它们,以及计算所有总代码行数。然后,它旨在提供每个关键字的总数,因此可以根据关键字在文件中出现的频率计算百分比。

但是,我无法让代码识别关键字。我应该如何读取代码的总行数以及查找关键字?

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

#define _CRT_SECURE_NO_WARNINGS

/*  Count and compute:

    number of total lines
    number and percentage of blank lines
    number and percentage of comments (start with // or /*)
    number and percentages of ints, longs, floats, doubles, char
    number and percentages of if's
    number and percentage of else's
    number and percentage of for's
    number and percentage of switch
    number and percentage of semicolons
    number and percentage of structs
    number and percentage of arrays (contains [ or ], divide count by 2)
    number of blocks (contains { or }, divide count by 2)
*/


int main(void)
{
    int lineCount = 0;  // Line counter (result) 
    int forCount = 0; // For counter
    int intCount = 0;
    char c;

    FILE *ptr_file;
    char buf[1000];

    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {


        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == '\n') // Increment count if this character is newline 
                lineCount = lineCount + 1;
        }
    }
    fclose(ptr_file);
    //End of first scan
    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {
        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == 'for') // Increment count if this character is for
                forCount = forCount + 1;
        }
    }
    fclose(ptr_file);
    //End of second scan
    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {
        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == 'int') // Increment count if this character is for
                intCount = intCount + 1;
        }
    }

    fclose(ptr_file);
    printf("\nThe file has %d lines\n", lineCount);
    printf("\nThe file has %d fors\n", forCount);
    printf("\nThe file has %d ints\n", intCount);
}

【问题讨论】:

  • #defineing _CRT_SECURE_NO_WARNINGS 包含标准标头后无效。你必须在包含头文件之前定义它。
  • 对于大多数你想要计算的东西,你必须构建一个 AST。考虑使用 libclang。

标签: c fopen fgets getc


【解决方案1】:

你需要使用sscanf并逐行解析。

对于找到的每件物品,保持一个计数应该没有问题。

但正如您所讨论的(在其他论坛上寻求帮助),您需要的功能就是这个。

【讨论】:

    【解决方案2】:

    获得准确答案可能需要比您想象的更复杂的解析:考虑一下long 也可以声明为long int,并且long longlong long int 也是有效的变量声明.此外,您可以在同一行声明多个变量,并且您不想计算 int 是较长单词一部分的实例。

    为了快速近似,Linux 工具 grepwc 可能会有所帮助:

    • wc -l filename 将列出文件的行数
    • grep "for" filename | wc -l 将列出包含 for 的行数

    请注意,这些是近似值:如果for 在一行中出现不止一次,或者for 是另一个词(如forth)的一部分,则仍会计算一个实例。

    【讨论】:

      猜你喜欢
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2023-03-31
      • 1970-01-01
      • 2011-03-22
      • 2013-04-27
      相关资源
      最近更新 更多