程序不读取.c文件

时间:2017-05-03 20:51:06

标签: c file

我一直在关注K& R书中的一些例子,以提高我的编程技巧。这恰好是例1-24。

我编写了一个程序来检查C程序的语法错误,例如无法匹配的括号,括号,注释等。我的问题是,当我运行程序时,它似乎陷入无限循环而且我和#39;我不完全确定原因。我认为它与EOF'有关,这是为了获取一个字符,直到文件结束。

(c = getchar()) != EOF

想法是根据数组中的哪个字符递增或递减变量。

我还不完全确定我是否正确打开.c文件。 .c文件与程序位于同一目录中,以确保它知道它所在的位置。

getLine()是由K& R编写的,但我自己也写了其他所有内容。

我的代码:

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>

/* Typedef -------------------------------------------------------------------*/
/* Define Contant-------------------------------------------------------------*/
#define MAX     1000

/* Macro ---------------------------------------------------------------------*/
/* Global variables ----------------------------------------------------------*/
/* Function prototypes -------------------------------------------------------*/
int getLine(char str[], int lim);
void printErrors(int parentheses, int braces, int brackets, 
                        int singleQ, int doubleQ, int comments);

//Checks for the following:
// () {} [] "" ' ' /* */

int main(void) 
{
    char line[MAX];
    int i, parentheses, braces, brackets, singleQ, doubleQ, comments;
    parentheses = braces = brackets = singleQ = doubleQ = comments = 0;

    //Open the file
    FILE *pFile = fopen("test.c", "r");

    while( getLine(line, MAX) > 0)
    {
        for(i = 0; line[i] != '\0'; ++i)
        {
        //check for syntax balance
            if( line[i] == '(' )
                ++parentheses;
            else if( line[i] == ')' )
                --parentheses;
            else if( line[i] == '{' )
                ++braces;
            else if( line[i] == '}' )
                --braces;
            else if( line[i] == '[' )
                ++brackets;
            else if( line[i] == ']' )
                --brackets;
            else if( line[i] == '\'' )
            {
                if( !singleQ )
                    ++singleQ;
                else
                    --singleQ;
            }
            else if( line[i] == '"' )
            {
                if( !doubleQ )
                    ++doubleQ;
                else
                    --doubleQ;
            }
            else if( (line[i] == '/') && (line[i+1] == '*') )
                ++comments;
            else if( (line[i] == '*') && (line[i+1] == '/') )
                --comments;

        }
    }

    fclose(pFile);

    printErrors(parentheses, braces, brackets, singleQ, doubleQ, comments);

    return 0;
}
void printErrors(int parentheses, int braces, int brackets, 
                        int singleQ, int doubleQ, int comments) 
{
    printf("\n\nPositive interger indicates an extra opening bracket\n");
    printf("\nNegative interger indicates an extra closing bracket\n\n");

    printf("Parenthesis Errors: %d\n", parentheses);
    printf("Brace Errors: %d\n", braces);
    printf("Bracket Errors: %d\n", brackets);
    printf("Single Quote Errors: %d\n", singleQ);
    printf("Double Quote Errors: %d\n", doubleQ);
    printf("Comment Errors: %d\n", comments);
}


int getLine(char str[], int lim)
{

    char c;
    int i = 0;

    while( (i < lim -1) && ((c = getchar()) != EOF) && (c != '\n') ) 
    {
        str[i] = c;
        ++i;
    }

    if( c == '\n' ) 
    {
        str[i] = '\n';
        ++i;
    }
    str[i] = '\0';
    return i;
}

编辑:我已将char c更改为int c,我还使用了fgetc(),并在函数getLine内移动了打开/关闭.c文件,但它仍然具有相同的行为。非常感谢任何其他建议。

int getLine(char str[], int lim)
{

    int c;
    int i = 0;

    //Open the file
    FILE *pFile = fopen("test.c", "r");

    while( (i < lim -1) && ((c = fgetc(pFile)) != EOF) && (c != '\n') ) 
    {
        str[i] = c;
        ++i;
    }

    if( c == '\n' ) 
    {
        str[i] = '\n';
        ++i;
    }
    str[i] = '\0';
    fclose(pFile);

    return i;
}

0 个答案:

没有答案