2012-09-30 7 views
1
#include <stdlib.h> 
#include <stdio.h> 
#include <getopt.h> 
#include "mylib.h" 
#include "tree.h" 

/* A boolean type which can be TRUE or FALSE */ 
typedef enum bool_e {FALSE, TRUE} bool_t; 

static void usage(char *progname); 
static void setup(int argc, char **argv, char **filename, bool_t *rbt, 
        bool_t *do_depth, bool_t *p_order, bool_t *output_graph); 
static void make_graph(char *filename, tree t); 

static void usage(char *prog_name) { 
    fprintf(stderr, "Usage: %s [OPTION]... <STDIN>\n\n%s%s", prog_name, 
      "Perform various operations using a hash-table. By default read\n" 
      "words from stdin and print them with their frequencies to stdout.\n\n" 
      " -d  prints the tree depth\n" 
      " -p  prints the tree in pre_order\n" 
      " -o FILENAME prints output to a specified file\n\n" 
      " -r  creates a rbt tree\n" 
      " -h  Display this message\n\n"); 
} 

static void print_key(char *key) { 
    printf("%s\n", key); 
} 

static void make_graph(char *filename, tree t) { 
    FILE *file = fopen(filename, "w"); 
    fprintf(stderr, "Creating dot file '%s'\n", filename); 
    tree_output_dot(t, file); 
    fclose(file); 
} 



/** 
* Handle options given on the command-line by setting a number of 
* variables appropriately. May call usage() if incorrect arguments 
* or -h given. 
* 
* @param argc the number of command-line arguments. 
* @param argv an array of strings contain the command-line arguments. 
* @param rbt is created if -r is given 
* @param the tree depth is printed if d is given 
* @param tree_outputfile_dot is called if -o output-filename is given 
*/ 

int main(int argc, char **argv) { 
    bool_t rbt = FALSE, do_depth = FALSE, p_order = FALSE, output_graph = FALSE; 
    tree_t tree_type = BST; 
    tree t; 
    char word[256]; 
    char *dot_filename; 

    setup(argc, argv, &dot_filename, &rbt, &do_depth, &p_order, &output_graph); 
    if (rbt) { 
    t = tree_new(RBT); 
    } else { 
     t = tree_new(tree_type); 
    } 
    while ((getword(word, sizeof word, stdin)) != EOF) { 
     t = tree_insert(t, word); 
    } 

    if (do_depth) { 
     printf("%d\n", tree_depth(t)); 
    } else if (p_order) { 
     tree_preorder(t, print_key); 
    } else if (output_graph) { 
     make_graph(dot_filename, t); 
    } else { 
     tree_inorder(t, print_key); 
    } 


    t = tree_free(t); 
    return EXIT_SUCCESS; 
} 


static void setup(int argc, char **argv, char **filename, bool_t *rbt, 
        bool_t *do_depth, bool_t *p_order, bool_t *output_graph) { 
    const char *optstring = "do:prh"; 

    char option; 

    while ((option = getopt(argc, argv, optstring)) != EOF) { 
     switch (option) { 
     case 'd': 
     *do_depth = TRUE; 
     break; 
     case 'o': 
     *filename = optarg; 
     *output_graph = TRUE; 
     break; 
     case 'p': 
     *p_order = TRUE; 
     break; 
     case 'r': 
     *rbt = TRUE; 
     break; 
     case 'h': 
     default: 
     usage(argv[0]); 
     exit(EXIT_SUCCESS); 
     } 
    } 
} 

모두 안녕하세요,이 코드를 컴파일하려고하는데 수정 방법을 모르는 문제가 있습니다. 내 파일 하나의 경고와 함께 컴파일하고, 나는 그것을 치료하고 싶습니다.컴파일러 경고, 파일 열기

을 main.c는 : 함수에서 '주' 을 main.c : 56 : 경고 : 'dot_filename'내가이 고정 주위를 얻을 얼마나 정말 모르겠어요이 기능

에서 초기화되지 않은 사용할 수 있습니다 문제는 일반적으로 파일을 자주 여는 작업을하지 않고 무엇을해야할지 잘 모르겠습니다. 이 경고가 사라지게하려면 어떻게해야합니까?

+0

"dot_filename"을 선언 한 위치를 찾고 초기 값을 지정 하시겠습니까? 'char * dot_filename = NULL'; 물론 * 실제로 * 사용하려고 시도하기 전에 * 할당하십시오.) – paulsm4

답변

4

단순히 null 포인터로 dot_filename을 초기화 할 수 있습니다.

char *dot_filename = NULL; 

경고를 수정해야합니다.