在函数'print_usage'中:错误:预期')'在'PROGRAM'之前

时间:2018-01-30 03:21:01

标签: c

正如标题所述,当我尝试构建我的项目时,我收到一个错误,其中指出:在函数'print_usage'中:错误:预期')'在'PROGRAM'之前

我不确定导致此错误的原因,并希望有人能指出我正确的方向。

#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"

void print_usage();
void load_file(char *filename, char **buffer, size_t *size);

int main(int argc, char *argv[]) {

    argc--;
    argv++;

    if(argc <= 0) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    while(argc > 0) {
        size_t size; /* I'm not using this, but it could be useful. */
        Token token;
        char *buffer = NULL;
        unsigned int token_cnt = 0;

        load_file(*argv, &buffer, &size);
        if(buffer) {
            char *b;  /* editable copy of the buffer pointer */
            b = buffer;
            while(get_token(&b, &token) != TOKEN_EOF) {
                printf("%s: ", id_string[token.id]);
                if(token.str) {
                    printf("\"%s\"\n", token.str);
                    free(token.str);
                }
                else {
                    printf("no string found\n");
                }

                if(token.id != TOKEN_BAD) {
                    token_cnt++;
                }
            }
            printf("\n----------------------\n");
            printf("%u valid tokens found\n", token_cnt);
            printf("----------------------\n");

            free(buffer);
        }
        argc--;
        argv++;
    }

    exit(EXIT_SUCCESS);
}

void print_usage() {

    fprintf(stderr, "Usage:   " PROGRAM " [file]...\n");

    return;
}

2 个答案:

答案 0 :(得分:2)

argc--;
argv++;
/* Since you just incremented argv, you just lost the program name! */
/* You need argv[0] as the program name to create the error message */

if(argc <= 0) {   
    print_usage( argv[0] );
    exit(EXIT_FAILURE);
}
void print_usage(char* program) {

    fprintf(stderr, "Usage: %s [file]...\n", program);
}

答案 1 :(得分:0)

查找fprintf()的语法,你可能需要更像

的东西
void print_usage() {

    fprintf(stderr, "Usage: %s [file]...\n", PROGRAM);

    return;
}

假设PROGRAM在某处被定义为字符串。