#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'中:main.c:56:警告:'dot_filename'可能在此函数中未初始化使用
我不确定如何解决这个问题,我通常不经常使用文件打开的东西,而且我不确定该怎么做。我该怎么做才能让这个警告消失?