那是什么?表示使用printf输出字符串时strcmp是否应该为匹配返回零(在C中)?

时间:2012-02-21 19:32:51

标签: c strcmp

HW点是应该从文件中读取它:

int func(int arg) { int x = 7; char c = 'a'; arg = x + c; return arg; }

并输出:

func, arg, x, c
//or optionally also the next line
int, char, return

所以我的问题是在我的输出上出现应该出现的字符的问号,并且strcmp没有为我的if语句返回零,这要求它起作用(注意:我有很多评论和printf来帮助我弄清楚我哪里出错了):

while((d=fgetc(function_file)) != EOF) {
    //start by checking for any defines and just looping until a new line character comes up
    if( d == '#')
        flag = true;

    if((d == '\n') && (flag)){
        //flag which says if I am searching for a newline but only after a define/include 
        //was found will I say the flag is not needed anymore to allow normal parsing
        flag = false;
    } //end of check for a define function

    if( (flag == false) && (d != '#') ) {

        //this is where the main chunk of code goes to do all the parsing
        if((d != ' ') && (d != '\t') && (d !='\n') && (d != '{') && (d != '}') && (d != '(') && (d != ')') && (d != '*') && (d != '=') && (d != '+')) {

            printf("Character read is : %c\n", d);
            start = true;
            temp[count] = c;
            count++;
        }


    }//end of main chunk of code
        if((start == true) && ((d == ' ') || (d == '(') || (d == ')') || (d == '{') || (d == '}'))) {
            //end a string and compare it hear
            if(match == false) {
            temp[count] = '\0';
        printf("String: %s\n", temp);//*********************************DEBUGGING***********

            start = false;
            int compare;
            for(compare = 0; compare < key_counter; compare++) {
                int optimus;
                optimus = strcmp(keywords[compare], temp); //************** ONE OF THE ERRORS IS HERE***************************************?
                if(optimus == 0){
                    //printf("this is actually runnning");//*********************************DEBUGGING***********
                    int len = strlen(temp);
                    bizarro_keywords[bizarro_key_counter] = (char *)malloc(sizeof(char) * (len +1));
                    memcpy(bizarro_keywords[bizarro_key_counter], temp, len +1);
                    printf("\nWhats inside bizarro_key_counter right after it is allocated memory: %s", bizarro_keywords[bizarro_key_counter]);
                    bizarro_key_counter++;
                    match = true;
                }

            }

            int x;
            for(x = 0; x < count; x++)
                temp[x] = '\0';
            count = 0;


        } else { //if match equals true just grab the next available string 
            //printf("is this one ever running?");
            temp[count] = '\0';
            start = false;
            printf("String: %s\n", temp);

                    int len = strlen(temp);
                    identifiers[iden_counter] = (char *)malloc(sizeof(char) * (len +1));
                    memcpy(identifiers[iden_counter], temp, len +1);
                    iden_counter++;
                    match = false;



                    int x;
                    for(x = 0; x < count; x++)
                        temp[x] = '\0';
                    count = 0;

            }   
        }

}//end of while loop for reading the whole file

这是我的输出:

Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : f
Character read is : u
Character read is : n
Character read is : c
String: ????
Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : a
Character read is : r
Character read is : g
String: ???
Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : x
String: ?
Character read is : 7
Character read is : ;
String: ??
Character read is : c
Character read is : h
Character read is : a
Character read is : r
String: ????
Character read is : c
String: ?
Character read is : '
Character read is : a
Character read is : '
Character read is : ;
String: ????
Character read is : a
Character read is : r
Character read is : g
String: ???
Character read is : x
String: ?
Character read is : c
Character read is : ;
String: ??
Character read is : r
Character read is : e
Character read is : t
Character read is : u
Character read is : r
Character read is : n
String: ??????
Character read is : a
Character read is : r
Character read is : g
Character read is : ;
String: ????

我是C的新手,我迷失了为什么我得到了那个输出。请提示。

2 个答案:

答案 0 :(得分:1)

据我所知,你只想从输入文件中读取行并将其拆分为标记。您可以使用strtok函数而不是从文件中读取字符:

char* keywords[] = { "int", "char", "return" };
int i = 0, j, keywordsCount = 3;
FILE* f = fopen("a.txt", "r");
char line[1000], *token;
while (fgets(line, 1000, f) != NULL) // read line
{
    char* token = strtok(line, " \t\n{}()*+=,;");
    while (token != NULL)
    {
        printf("String %d:%s", i++, token);

        for (j = 0; j < keywordsCount; ++j)
            if (strcmp(token, keywords[j]) == 0)
            {
                printf(" <-- Look, it's keyword!");
                break; // breaks for, not while
            }

        putchar('\n');
        token = strtok(NULL, " \t\n{}()*+=,;");
    }
}

请注意,我在分隔符字符串中使用'\n'字符,因为fgets函数会在最后包含'\n'的缓冲区中读取行。

文件内容a.txt

int func(int arg) { int x = 7; char c = 'a'; arg = x + c; return arg; }

输出:

String 0:int <-- Look, it's keyword!
String 1:func
String 2:int <-- Look, it's keyword!
String 3:arg
String 4:int <-- Look, it's keyword!
String 5:x
String 6:7
String 7:char <-- Look, it's keyword!
String 8:c
String 9:'a'
String 10:arg
String 11:x
String 12:c
String 13:return <-- Look, it's keyword!
String 14:arg

答案 1 :(得分:0)

您的代码有点难以阅读 - 至少第四部分(尽管有评论)因为它太长了。

你应该将你的功能分成几个较小的功能,你甚至已经有了第1-4步的结构。现在,因为你重新使用之前声明的变量,当你进入第四步时可能会偷偷摸摸count而不是= = 0这样的问题。因为有太多的扭曲和转动它有点难以看到问题

当你进行这样的词法分析时,通常最好使用状态机,切换语句对此有用,例如

typedef enum { Idle, Include , ... } states_t;
states_t state=Idle;

switch (state)
{
   case Idle:
      switch ( d )
      { 
         case '#':
           state = Include;
           break;
     ...
     break;
   case Include:
     ...
     break;

      break;

如果您没有调试器,请包含assert.h并在您的代码中放置断言以确保捕获所有假设,即assert( count == 0 );在第4步之前可能是件好事。

在复制时使用strcpy或更好strncpy而不是memcpy,strcpy - 系列在遇到\ 0更有效时会停止复制(它会复制\ 0以及。)