【问题标题】:C program for printing function details of a C file [closed]用于打印 C 文件的功能详细信息的 C 程序 [关闭]
【发布时间】:2012-12-23 06:18:20
【问题描述】:

对于给定的 C 代码,我想打印函数名称和每个函数的行数。

下面是我的代码:

struct fundetails
{
    int nooflines;
    char funcname[SIZE];
}s[SIZE];


char *ffname(char *line,char *name)
{

    int i=1,j=0;
    char *dt; 

    strtok(line,"("); 
    dt = strchr(line,' '); 
    if(dt[i] == '*')
        i++;
    while(dt[i] != '\0')
    {
        name[j]=dt[i];
        i++;
        j++;
    }
    name[j] ='\0';
    return NULL;  //...
}
int main(int argc, char **argv)
{
    if(argc < 2)
    {
        printf("Give the filename \n");
        printf("Usage: %s filename\n", argv[0]);
        return -1;
    }
    int i, lines =0, funlines =0,count =0, fn =0, flag =0, fg=0,size=0,emptyflag=0,commandflag=0;
    char c[SIZE],b[SIZE],st[SIZE],d[SIZE];
    char *fname;
    int command[]={};
    FILE *fd;
    fd = fopen(argv[1],"r");
    while(fgets(c,SIZE,fd))
    {
        emptyflag=0;    
        lines++;
        for(i=0;i<(sizeof(command)/4);i++)
        {
            if(lines == command[i])
            {
                commandflag=1;
                break;
            }   
        }
        strcpy(st,c);
        strtok(st," ");
        size = strlen(c);
        if(size == 1 && (strcmp(c,"\n"))== 0)// Checking for empty line
            emptyflag=1;
        if( !strcmp(st,"struct")) // Checking whether line is structure or struct function
            fg=1;
        for(i=0;i<size;i++)
        {
            if(commandflag)
            {
                break;
            }   
            while( c[i] =='\t' || c[i] == ' ')
            {
                i++;
            }
            if( c[i] == '{')
            {
                count++;
                if(flag)
                {
                    if(!emptyflag)
                        funlines++;
                    else
                        emptyflag=0;
                }
                if(count ==1 && fg ==1)
                {
                    if(b[strlen(b)-2] == ')')
                    {
                        fn++;
                        printf("Dhahira");
                        printf("Function %d is Started..............\n", fn); 

                        flag = 1;
                        printf("Dhahira");
                        ffname(b,fname);
                        printf("Function name is:%s\n",fname);
                    }   
                    else
                    {
                        count--;
                    }   
                }
                else if(count == 1)
                {
                    fn++;
                    printf("Function %d is Started..............\n", fn); 
                    flag = 1;
                    ffname(b,fname);//...
                    printf("Function name is:%s\n",fname);
                }
                break;
            }
            else if( c[i] == '}')
            {
                count--;
                if(count ==0 && fg ==1)
                { 
                    flag = 0;
                    printf("No of lines in the function %d is: %d\n", fn, funlines);
                    printf("Function %d is finished..........\n", fn);
                    s[fn-1].nooflines=funlines;//...
                    strcpy(s[fn-1].funcname,fname);//..
                    funlines = 0;
                    fg=0;
                }
                else if(count ==0)
                {
                    flag = 0;
                    printf("No of lines in the function %d is: %d\n", fn, funlines);
                    printf("Function %d is finished..........\n", fn);
                    s[fn-1].nooflines=funlines;//...
                    strcpy(s[fn-1].funcname,fname);//..
                    funlines = 0;
                }
                else if(count == -1)
                {
                    count=0;
                    fg=0;
                }
                else 
                {
                    if(!emptyflag)
                        funlines++;
                    else
                        emptyflag=0;
                }
                break;
            }
            else if(flag ==1 && fg==1)
            {
                if(!emptyflag)
                    funlines++;
                else
                    emptyflag=0;
                break;
            }
            else if(flag)
            {
                if(!emptyflag)
                    funlines++;
                else
                    emptyflag=0;
                break;
            }
            break;
        }
        if(commandflag == 1)
            commandflag = 0;
        else
            strcpy(b,c);
    }

    printf("FUN_NAME\tNO_OF_LINES\n");
    for(i=0;i<fn;i++)
    {
    printf("%s\t\t%d\n",s[i].funcname,s[i].nooflines);
    }
    return 0;
}

我得到以下输出。

Function 1 is Started..............
Segmentation fault (core dumped)

我认为ffname 函数可能是一个原因。但是,当我在printf("Function %d is Started..............\n", fn); 行上方包含一个 printf 语句时,它仍然不会打印该行。所以,我无法弄清楚这个问题背后的真正原因。请指导我解决这个问题。

【问题讨论】:

  • 您需要学习如何使用调试器。很快!
  • 你应该用gcc -Wall -g编译,改进你的代码直到没有警告,并学会使用gdbvalgrind来调试它;在您的代码中 fname 似乎永远不会正确初始化。

标签: c linux segmentation-fault structure coredump


【解决方案1】:

分段错误是由于未初始化使用fname。将其声明为数组:

char fname[256]; // or dynamically allocate

它应该可以工作。您看不到 printf 输出,因为此函数写入缓冲区并且不会立即输出您写入的内容。在分段错误之后,缓冲的输出消失了。您可以在printf 之后使用fflush(stdout) 强制立即输出。

如果您使用-Wall 标志编译代码,您也会看到一条警告消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 2014-03-03
    • 2017-08-01
    • 2012-03-19
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多