Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 识别函数签名_C_File_File Io - Fatal编程技术网

C 识别函数签名

C 识别函数签名,c,file,file-io,C,File,File Io,我正在用c语言编写一个程序来识别函数的签名并将其复制到另一个文件中 其思想是确定任何一行中的括号,并将该行复制到一个文件中。 之后,我们可以检查返回类型和参数,以便区分构造if、while和除main之外的用户定义函数 但我的代码卡在无限循环中。找不到问题 查找信号c #include <stdio.h> int main() { int count=0; char c; FILE *f1,*f2; f1=fopen("input.c","r"); f2=fopen("output.

我正在用c语言编写一个程序来识别函数的签名并将其复制到另一个文件中

其思想是确定任何一行中的括号,并将该行复制到一个文件中。 之后,我们可以检查返回类型和参数,以便区分构造if、while和除main之外的用户定义函数

但我的代码卡在无限循环中。找不到问题

查找信号c

#include <stdio.h>
int main()
{
int count=0;
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
 {
    do
    {
        if(c=='(')
        {
            fseek(f1,-count,1);
            do{
                putc(c,f2);

            }while((c=getc(f1))!=')');
            count=0;
        }
        else
            count++;

    }while((c=getc(f1))!=10);
    count=0;
 }
fclose(f1);
fclose(f2);
return 0;
}
#包括
int main()
{
整数计数=0;
字符c;
文件*f1,*f2;
f1=fopen(“input.c”、“r”);
f2=fopen(“output.c”,“w”);
而((c=getc(f1))!=EOF)
{
做
{
如果(c=='(')
{
fseek(f1,-计数,1);
做{
putc(c,f2);
}而((c=getc(f1))!=');
计数=0;
}
其他的
计数++;
}而((c=getc(f1))!=10);
计数=0;
}
fclose(f1);
fclose(f2);
返回0;
}
投入.c

#include<stdio.h>
void fun();
int main()
{
  fun();
  return 0;
}

void fun()
{
  printf("hello");
}
#包括
虚无乐趣();
int main()
{
乐趣();
返回0;
}
虚无乐趣()
{
printf(“你好”);
}
确定函数签名的任何其他想法都将非常有用。

我已经找到了答案

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

char str1[50];
int count=0,i=0;
int main()
{
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");

while((c=getc(f1))!=EOF)
{
   if(c!=10)                                    //checks for /n
   {
       if(c=='(')
       {
           ++count;
           fseek(f1,-count,1);              //moves f1 to 'count' bytes back i.e. beginning of line
           i=0;
           while((c=getc(f1))!=';'&&c!='{') //checks for declaration or definition
           {
              str1[i++]=c;
           }

           if(strstr(str1,"main(")!=NULL)   //checks whether str1 contains main 
               return 0;
           else
               {
               fprintf(f2,"%s",str1);   // copies str1 in f2
               count=0;
               }
       }
       else
       count++;
   }

   else
       count=0;
   if(c==10)
         putc(c,f2);

}

fclose(f1);
fclose(f2);
return 0;
}
#包括
#包括
char-str1[50];
整数计数=0,i=0;
int main()
{
字符c;
文件*f1,*f2;
f1=fopen(“input.c”、“r”);
f2=fopen(“output.c”,“w”);
而((c=getc(f1))!=EOF)
{
如果(c!=10)//检查/n
{
如果(c=='(')
{
++计数;
fseek(f1,-count,1);//将f1移回“count”字节,即行的开头
i=0;
while((c=getc(f1))!=';'&&c!='{')//检查声明或定义
{
str1[i++]=c;
}
if(strstr(str1,“main”()!=NULL)//检查str1是否包含main
返回0;
其他的
{
fprintf(f2,“%s”,str1);//在f2中复制str1
计数=0;
}
}
其他的
计数++;
}
其他的
计数=0;
如果(c==10)
putc(c,f2);
}
fclose(f1);
fclose(f2);
返回0;
}
我想出来了

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

char str1[50];
int count=0,i=0;
int main()
{
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");

while((c=getc(f1))!=EOF)
{
   if(c!=10)                                    //checks for /n
   {
       if(c=='(')
       {
           ++count;
           fseek(f1,-count,1);              //moves f1 to 'count' bytes back i.e. beginning of line
           i=0;
           while((c=getc(f1))!=';'&&c!='{') //checks for declaration or definition
           {
              str1[i++]=c;
           }

           if(strstr(str1,"main(")!=NULL)   //checks whether str1 contains main 
               return 0;
           else
               {
               fprintf(f2,"%s",str1);   // copies str1 in f2
               count=0;
               }
       }
       else
       count++;
   }

   else
       count=0;
   if(c==10)
         putc(c,f2);

}

fclose(f1);
fclose(f2);
return 0;
}
#包括
#包括
char-str1[50];
整数计数=0,i=0;
int main()
{
字符c;
文件*f1,*f2;
f1=fopen(“input.c”、“r”);
f2=fopen(“output.c”,“w”);
而((c=getc(f1))!=EOF)
{
如果(c!=10)//检查/n
{
如果(c=='(')
{
++计数;
fseek(f1,-count,1);//将f1移回“count”字节,即行的开头
i=0;
while((c=getc(f1))!=';'&&c!='{')//检查声明或定义
{
str1[i++]=c;
}
if(strstr(str1,“main”()!=NULL)//检查str1是否包含main
返回0;
其他的
{
fprintf(f2,“%s”,str1);//在f2中复制str1
计数=0;
}
}
其他的
计数++;
}
其他的
计数=0;
如果(c==10)
putc(c,f2);
}
fclose(f1);
fclose(f2);
返回0;
}

您必须正确解析C。这不是小事。也许可以查看libclang?@H2CO3 libclang是什么?谷歌是什么?请参阅“libclang”的第一个匹配项。也许这有帮助:@LorenzoDonati抱歉问了个愚蠢的问题,但我从来没听说过libclang这个词。所以我在谷歌上搜索了它,找到了一些教程。阅读它们。你必须正确解析C。这不是小事。也许可以看看libclang?@H2CO3什么是libclang谷歌?看看“libclang”的第一个匹配项也许这有帮助:@LorenzoDonati很抱歉问了一个愚蠢的问题,但我从来没有听说过libclang这个词。所以我在谷歌上搜索了一下,找到了一些教程。阅读它们。