revise file handling code
authorYoungjae Shin <yj99.shin@samsung.com>
Wed, 15 Sep 2021 08:26:07 +0000 (17:26 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 15 Sep 2021 10:12:33 +0000 (19:12 +0900)
Change-Id: I42536e82266e4d5cab39ebdb031f85c36561b4e0

subprojects/libbeyond-runtime_tflite/src/main.cc
tools/evaluation/tasks/inference_diff/inference_diff.cc
tools/evaluation/tasks/inference_peer/tizen.native/image_classification.cc

index f05acd3e8cc19d23d200e9d4fa35011e6e9a927f..8977590e084beab1ed24a9b84a129e38d0e4ab54 100644 (file)
@@ -157,27 +157,28 @@ int main(void)
                 }
 
                 if (optarg != nullptr) {
-                    FILE *fp = fopen(optarg, "r");
-                    if (fp != nullptr) {
-                        fseek(fp, 0L, SEEK_END);
-                        input[input_idx].size = ftell(fp);
-                        fseek(fp, 0L, SEEK_SET);
+                    FILE *input_fp = fopen(optarg, "r");
+                    if (input_fp != nullptr) {
+                        fseek(input_fp, 0L, SEEK_END);
+                        input[input_idx].size = ftell(input_fp);
+                        fseek(input_fp, 0L, SEEK_SET);
 
                         input[input_idx].data = malloc(input[input_idx].size);
                         if (input[input_idx].data == nullptr) {
                             char errMsg[80] = { '\0' };
                             fprintf(stderr, "malloc: %s\n", strerror_r(errno, errMsg, sizeof(errMsg)));
                         } else {
-                            if (fread(input[input_idx].data, input[input_idx].size, 1, fp) != 1) {
+                            if (fread(input[input_idx].data, input[input_idx].size, 1, input_fp) != 1) {
                                 char errMsg[80] = { '\0' };
-                                fprintf(stderr, "fread: %s\n", strerror_r(ferror(fp), errMsg, sizeof(errMsg)));
+                                fprintf(stderr, "fread: %s\n", strerror_r(ferror(input_fp), errMsg, sizeof(errMsg)));
                                 _exit(EIO);
                             }
                         }
 
-                        if (fclose(fp) < 0) {
+                        if (fclose(input_fp) < 0) {
                             char errMsg[80] = { '\0' };
                             fprintf(stderr, "Unable to close the file pointer: %s\n", strerror_r(errno, errMsg, sizeof(errMsg)));
+                            _exit(EIO);
                         }
                     }
                 }
index 91c95bd6333ba699c2a76efd0a0fc93dfe933ff9..b4bf90ee518cfc07e2629fdac994362e3ff4c2a4 100644 (file)
@@ -106,11 +106,14 @@ bool InferenceOutputDiff::CheckCmdline()
         printf("Incorrect Model file\n");
         result = false;
     }
+    model_check.close();
+
     std::ifstream input_check(input_tensor_path_);
     if (input_check.good() == false) {
         printf("Incorrect Input file\n");
         result = false;
     }
+    input_check.close();
 
     printf("Model File : %s \n", model_file_path_.c_str());
     printf("Input File : %s \n", input_tensor_path_.c_str());
index a8c459cc5c30b87bb93f8ae40f7ade267c8e9f5f..0bc86f2d20e6759e6b4a7a0c81098bfb1bd8515a 100644 (file)
@@ -258,16 +258,21 @@ bool ImageClassification::CheckCmdline()
             printf("Incorrect Model file\n");
             result = false;
         }
+        model_check.close();
+
         std::ifstream input_check(input_image_path_);
         if (input_check.good() == false) {
             printf("Incorrect Input Image file\n");
             result = false;
         }
+        input_check.close();
+
         std::ifstream output_check(output_label_path_);
         if (output_check.good() == false) {
             printf("Incorrect Output Label file\n");
             result = false;
         }
+        output_check.close();
 
         if (LoadLabels() == false) {
             printf("Failed to load labels\n");