c - 一个文件中注释的字符数(C编程)

标签 c file count comments

我似乎做错了,尝试了一切,但是..

int commentChars() {
char str[256], fileName[256];
FILE *fp;
int i;


do{
    long commentCount=0;
    fflush(stdin);
    printf("%s\nEnter the name of the file in %s/", p, dir);
    gets(fileName);

    if(!(fp=fopen(fileName, "r"))) {
            printf("Error! File not found, try again");
                return 0;
    }

    while(!feof(fp)) {
            fgets(str,sizeof str,fp);
            for(int i=0;i<=sizeof str;i++) {
                if(str[i] == '/' && str[i+1] == '/') {
                        commentCount += (strlen(str)-2);
                }
            }
    }

    fclose(fp);

        printf("All the chars, contained in a comment: %ld\n", commentCount);
        puts(p);
        printf("Do you want to search for another file?<Y/N>: ");
        i=checker();


}while(i);}

结果是“评论中包含的所有字符:0”,即使我有评论。 我的第二个问题是......类似地,我如何才能对包含/* */的评论做同样的事情,这对我来说似乎是一项不可能完成的工作。

最佳答案

我认为您最好使用正则表达式。他们看起来很可怕,但对于这样的事情他们真的没有那么糟糕。您总是可以尝试打一些正则表达式高尔夫来练习 ;-)

我会按如下方式处理它:

  • 构建一个捕获评论的正则表达式
  • 扫描你的文件
  • 计算匹配中的字符数

使用一些 regex code还有一点关于匹配 comments in C ,我把它一起破解了,这应该允许你计算 block 样式注释/* */中的所有字节 - 包括分隔符。我只在 OS X 上测试过它。我想你可以处理剩下的事情吧?

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

#define MAX_ERROR_MSG 0x1000

int compile_regex(regex_t *r, char * regex_text)
{
    int status = regcomp (r, regex_text, REG_EXTENDED|REG_NEWLINE|REG_ENHANCED);
    if (status != 0) {
        char error_message[MAX_ERROR_MSG];
        regerror (status, r, error_message, MAX_ERROR_MSG);
        printf ("Regex error compiling '%s': %s\n",
            regex_text, error_message);
        return 1;
    }
    return 0;
}
int match_regex(regex_t *r, const char * to_match, long long *nbytes)
{
    /* Pointer to end of previous match */
    const char *p = to_match;
    /* Maximum number of matches */
    size_t n_matches = 10;
    /* Array of matches */
    regmatch_t m[n_matches];

    while(1) {
        int i = 0;
        int nomatch = regexec (r, p, n_matches, m, 0);
        if(nomatch) {
            printf("No more matches.\n");
            return nomatch;
        }
        //Just handle first match (the entire match), don't care
        //about groups
        int start;
        int finish;
        start = m[0].rm_so + (p - to_match);
        finish = m[0].rm_eo + (p - to_match);
        *nbytes += m[0].rm_eo - m[0].rm_so;

        printf("match length(bytes) : %lld\n", m[0].rm_eo - m[0].rm_so);
        printf("Match: %.*s\n\n", finish - start, to_match + start);
        p += m[0].rm_eo;
    }
    return 0;
}

int main(int argc, char *argv[])
{
    regex_t r;
    char regex_text[128] = "/\\*(.|[\r\n])*?\\*/";
    long long comment_bytes = 0;

    char *file_contents;
    size_t input_file_size;
    FILE *input_file;
    if(argc != 2) {
        printf("Usage : %s <filename>", argv[0]);
        return 0;
    }
    input_file = fopen(argv[1], "rb");
    fseek(input_file, 0, SEEK_END);
    input_file_size = ftell(input_file);
    rewind(input_file);
    file_contents = malloc(input_file_size * (sizeof(char)));
    fread(file_contents, sizeof(char), input_file_size, input_file);

    compile_regex(&r, regex_text);
    match_regex(&r, file_contents, &comment_bytes);
    regfree(&r);
    printf("Found %lld bytes in comments\n", comment_bytes);

    return 0;
}

关于c - 一个文件中注释的字符数(C编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221190/

相关文章:

C:变量在使用后声明的作用/意义是什么?

c - 发送十六进制代码

c - C 中的循环不能正确递增

python - 使用字典计算列表中的项目

c++ - 对于在 C、C++、Linux 应用程序开发方面拥有超过 8 年经验的候选人,我们应该寻找什么?

c# 打开文件,路径以 %userprofile% 开头

c - (C Linux) 中的 read() 和缓冲区大小错误

python - 将列表转换为 str 并写入新的 .mtx 文件

mysql - 从多个表和字符串中选择

c# - LINQ如何统计符合条件的元素个数