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」可用於初始化在這個函數
我真的不知道我將如何得到解決這個固定問題,我通常不會經常使用文件打開的東西,而且我也不確定該怎麼做。我會做什麼來使這個警告消失?
找到你聲明「dot_filename」的地方,並給它一個初始值? 'char * dot_filename = NULL';當然,在你嘗試*使用它之前,實際上*分配*);) – paulsm4