文件中注释的字符数(C编程) [英] The number of characters of comments in a file (C programming)

查看:169
本文介绍了文件中注释的字符数(C编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法正确,尝试一切,但...

I can't seem to get it right, tried everything, but..

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,即使我有评论。
我的第二个问题是..类似地,我如何做同样的评论,包含/ * * /,似乎是一个不可能的工作,对我来说。

The result is "All the chars, containted in a comment: 0", even though I have comments. And my second question was.. Analogically, how can I do the same for comments, containing /* */, seems like an impossible job for me.

推荐答案

我认为你最好使用正则表达式。他们似乎很可怕,但他们真的不是那么坏的这样的事情。你可以随时尝试打一些正规高尔夫球练习; - )

I think you best use regular expressions. They seem scary, but they're really not that bad for things like this. You can always try playing some regex golf to practice ;-)

我会这样处理:


  • 创建捕获注释的正则表达式

  • 扫描文件

  • 计算匹配中的字符

  • Build a regular expression that captures comments
  • Scan your file for it
  • Count the characters in the match

使用一些正则表达式代码和一些关于匹配 C中的评论,我一起黑客它应该允许您计数作为块样式注释的一部分的所有字节/ * * / - 包括分隔符。我只在OS X上测试它。我想你可以处理剩下的?

Using some regex code and a bit about matching comments in C, I hacked this together which should allow you to count all the bytes that are part of a block style comment /* */ - Including the delimiters. I only tested it on OS X. I suppose you can handle the rest?

#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编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆