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 - Fatal编程技术网

程序未读取.c文件

程序未读取.c文件,c,file,C,File,我一直在遵循K&R书中的一些例子来提高我的编程技能。这恰好是示例1-24 我编写了一个程序,检查C程序的语法错误,如不匹配的括号、括号、注释等。我的问题是,当我运行程序时,它似乎陷入了无限循环,我不完全确定原因。我认为这与“EOF”有关,即假定在到达文件末尾之前获得一个字符 (c = getchar()) != EOF 其思想是根据数组中的字符增加或减少变量 我也不完全确定是否正确打开了.c文件。.c文件与程序位于同一目录中,以确保它知道自己的位置 getLine()是由K&R编写的,但其他所

我一直在遵循K&R书中的一些例子来提高我的编程技能。这恰好是示例1-24

我编写了一个程序,检查C程序的语法错误,如不匹配的括号、括号、注释等。我的问题是,当我运行程序时,它似乎陷入了无限循环,我不完全确定原因。我认为这与“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;
}

你是在读文件还是标准数据?你有
charc
-如果plain
char
是无符号类型,则永远无法与EOF true进行比较。请参阅:。使用
intc-始终。然而,正如@BLUEPIXY所指出的,您的
getLine()
函数是从标准输入读取的,而不是您打开的文件流。您需要修改代码,将文件流传递给
getLine()
函数,然后使用它读取字符。然后,您可能会遇到我诊断的问题。您使用
fgetc
而不是
getchar
和文件指针
pFile
。像以前一样在主程序中打开文件一次。将文件指针作为第三个参数传递给
getLine()
。与
fgets()
相比,这主要是因为通过每次在函数中打开和关闭文件,可以继续重新读取文件的第一行。
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;
}