From e9a15a37cfca436c928206d059e3ba15cafc1ccf Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Wed, 7 Nov 2012 14:41:15 -0800 Subject: [PATCH] ExUtilWriteFile() to write memory segment to file Change-Id: I4a93358e55ba5527667e017e178009251f64560e --- examples/example_util.c | 18 ++++++++++++++++++ examples/example_util.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/examples/example_util.c b/examples/example_util.c index 65655fd0..4cd2ce07 100644 --- a/examples/example_util.c +++ b/examples/example_util.c @@ -54,6 +54,24 @@ int ExUtilReadFile(const char* const file_name, return 1; } +int ExUtilWriteFile(const char* const file_name, + const uint8_t* data, size_t data_size) { + int ok; + FILE* out; + + if (file_name == NULL || data == NULL) { + return 0; + } + out = fopen(file_name, "wb"); + if (out == NULL) { + fprintf(stderr, "Error! Cannot open output file '%s'\n", file_name); + return 0; + } + ok = (fwrite(data, data_size, 1, out) == 1); + fclose(out); + return ok; +} + #if defined(__cplusplus) || defined(c_plusplus) } // extern "C" #endif diff --git a/examples/example_util.h b/examples/example_util.h index 54d1ebe2..7b30dbe1 100644 --- a/examples/example_util.h +++ b/examples/example_util.h @@ -23,6 +23,10 @@ extern "C" { int ExUtilReadFile(const char* const file_name, const uint8_t** data, size_t* data_size); +// Write a data segment into a file named 'file_name'. Returns true if ok. +int ExUtilWriteFile(const char* const file_name, + const uint8_t* data, size_t data_size); + #if defined(__cplusplus) || defined(c_plusplus) } // extern "C" #endif