Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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 - Fatal编程技术网

如何在c语言的文本输入文件中找到匹配括号或大括号的位置?

如何在c语言的文本输入文件中找到匹配括号或大括号的位置?,c,C,我正在用C.编写一个程序,它打开一个包含类似C的源代码的纯文本文件,读取该文件,并输出一个与第一个文件内容相同的文件,只是删除了所有注释。程序必须检查所有括号是否匹配,如果不匹配,程序应显示错误消息,在显示错误类型和遇到此错误的行号时。我显示了一条错误消息,但如何找到错误位置。。?输入和输出文件被传递到程序和行参数,如下所示: ./u可执行文件inputfile.txt outputfile.txt 以下是我编写的代码: #include <stdio.h> #include <

我正在用C.编写一个程序,它打开一个包含类似C的源代码的纯文本文件,读取该文件,并输出一个与第一个文件内容相同的文件,只是删除了所有注释。程序必须检查所有括号是否匹配,如果不匹配,程序应显示错误消息,在显示错误类型和遇到此错误的行号时。我显示了一条错误消息,但如何找到错误位置。。?输入和输出文件被传递到程序和行参数,如下所示: ./u可执行文件inputfile.txt outputfile.txt

以下是我编写的代码:

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

/* Functions */
void check_comment (char) ;  // checks for both types of comments, then passes on control to below comments
void block_comment () ;   //  handles block or multiline comments
void single_comment () ;   // handles single line comments

/* 2 file pointers - 1st is for the file in which we check for comments,
and 2nd is the file in which we copy the code after removing comments  */
FILE *fp , *fp2;

int main(void)
{
    char c;

    fp = fopen ("inputfile.txt","r") ;   // open the first file in read mode
    fp2 = fopen ("outputfile.txt","w") ;    // open the second file in write mode
    while((c=fgetc(fp))!=EOF)       // read the file character by character
        check_comment(c);   // check for each character if it seems like the beginning of a comment

     //  close both the files at the end of the program
    fclose(fp);
    fclose(fp2);

    FILE *fp;
    char fname[20];
    char brackets[20] = "{}[]()";
    int bracketCounts[6] = {0};
    char * found;
    int i;

    printf("Please enter the destination of the file: \n");
    scanf("%s", fname);

    if ((fp = fopen(fname, "r")) == NULL){
        printf("Problem opening file!\n");
        return 0x00;
    }

    printf("File opened correctly\n");

    // counting various parentheses
    while ((c = getc(fp)) != EOF){
        found = strchr(brackets, c);
        if (found != NULL) {
            bracketCounts[found - brackets]++;
        }
    }

    // dont't forget to close file after reading is done
    fclose(fp);

    // checking parentheses counters
    for (i=0; i < 6; i+=2) {
        if (bracketCounts[i] != bracketCounts[i+1]) {
            printf("Unbalanced parentheses !\n");
            return 0x00;
        }
    }

    printf("All parentheses are OK!\n");

    return 0;
}

// function that handles both types of comments
void check_comment(char c)
{
    char d;

    if( c == '/')   // if the character starts with '/', it 'could' be a comment
    {
        if((d=fgetc(fp))=='*')   // if the next character we read is '*', it is the beginning of multiblock comment
            block_comment();  // pass control to function that handles multiblock comments

        else if( d == '/')   // else if the next character we read is '/', it is the beginning of single line comment
        {
            single_comment();// pass control to function that handles single line comment

        }
        else
        {
            // if both the cases fail, it is not a comment, so we add the character as it is in the new file.
            fputc(c,fp2);
            fputc(d,fp2);
        }
    }

    // again, if all above fails, we add the character as it is in the new file.
    else
        fputc(c,fp2);
}


// function that handles block comments
void block_comment()
{

 char d,e;

    while((d=fgetc(fp))!=EOF)   // the block comment has started, read the character by character
    {
    /* keep reading the characters and do nothing,
    as they do not have to be copied into the new file (we are removing the comments)
    */
        if(d=='*')    // if the comment 'seems' like ending
        {
            e=fgetc(fp);  // check if it actually ends (block comments end with '*/')

            if(e=='/')  // if the comment 'has' ended, return from the function
                return;
        }
   }

}

// function that handles single line comments
void single_comment()
{
 char d,e;

    while((d=fgetc(fp))!=EOF)  // the single line comment has started, read the character by character
    {
    /* keep reading the characters and do nothing,
    as they do not have to be copied into the new file (we are removing the comments)
    */
        if(d=='\n')   // check if the comment ends (single comments end with '\n', or newline)
            return;  // if the comment 'has' ended, return from the function

    }

}

您可以实现或使用堆栈数据结构,在读取输入文件时,如果您得到{,或[push on the stack and if you get},或]pop the stack,则在输入文件的末尾,堆栈应为空,然后获得适当的匹配,否则会发生不匹配

除此之外,您还可以保留行号或位置等

例:1 2点左右 3点多

按,第1行,然后按,第2行,当你弹出时,第2行 如此类推,在这种情况下,如果你没有第二次收盘,你可以说,第1行
没有结束。

您到底想问我们什么?我们不是一个基础广泛的帮助服务:我们期望的问题相当狭窄。看,线路号?不。或者扫描缩进。运行编译器!systemgcc inputfile.txt;我的意思是,如果有不匹配的括号,有没有办法显示错误在哪里?为什么我们可以只使用整数计数器并递增或递减它?如果我们有{{…}我们如何确定前两个遗漏中的哪一个是它的一半呢?堆栈也只是一种递增的push和decrementpop,但与一个数组一起,可以在每次递增和递减时提取关于事物的信息,我们可以根据requirement@purec:{..}第一个案件显然结束了吗{是缺少的一个,如果我们像{1,{2当我们到达时}我们弹出{2}我们留下了一个{1`@Shivyaragati或第二个…实际上没有办法确定哪一个遗漏了最后一个。按照惯例,第一个通常被挑选出来报告。