簡體   English   中英

編譯器警告,打開文件

[英]Compiler warning, opening files

#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'可以在此函數中未初始化使用

我不太確定如何解決這個問題,我通常不會經常使用文件打開的東西,而且我不確定該怎么做。 我該怎么做才能使這個警告消失?

您只需使用空指針初始化dot_filename即可。

char *dot_filename = NULL;

這應該修復警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM