From 53b888b24b29fc9cc0f0d0c664e834c929aa5ac1 Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Mon, 10 Jun 2024 13:30:40 +0800 Subject: [PATCH] =?UTF-8?q?0.o=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/main.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++- Demo/test.csv | 3 +++ Demo/test.json | 5 ++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Demo/test.csv create mode 100644 Demo/test.json diff --git a/Demo/main.cpp b/Demo/main.cpp index 1e3ac95..562506b 100644 --- a/Demo/main.cpp +++ b/Demo/main.cpp @@ -1,15 +1,77 @@ #define SDL_MAIN_HANDLED #include +#include +#include +#include #include #include #include #include #include + +void test_json() +{ + std::ifstream file("test.json"); + if (!file.good()) + { + std::cout << "无法打开文件" << std::endl; + return; + } + + std::stringstream str_stream; + str_stream << file.rdbuf(); + file.close(); + + cJSON* json_root = cJSON_Parse(str_stream.str().c_str()); + + cJSON* json_name = cJSON_GetObjectItem(json_root, "name"); + cJSON* json_age = cJSON_GetObjectItem(json_root, "age"); + cJSON* json_pets = cJSON_GetObjectItem(json_root, "pets"); + + std::cout << json_name->string << ":" << json_name->valuestring << std::endl; + std::cout << json_age->string << ":" << json_age->valueint << std::endl; + + std::cout << json_pets->string << ":" << std::endl; + + cJSON* json_item = nullptr; + cJSON_ArrayForEach(json_item, json_pets) + { + std::cout << "\t" << json_item->valuestring << std::endl; + } +} + + +void test_csv() +{ + std::ifstream file("test.csv"); + if (!file.good()) + { + std::cout << "无法打开文件" << std::endl; + return; + } + + std::string str_line; + while (std::getline(file, str_line)) + { + std::string str_grid; + std::stringstream str_stream(str_line); + while (std::getline(str_stream, str_grid, ',')) + { + std::cout << str_grid << std::endl; + } + std::cout << std::endl; + } + file.close(); +} + int main() { + test_json(); + std::cout << "============================" << std::endl; + test_csv(); SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); @@ -18,7 +80,7 @@ int main() Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); -//创建窗口和与之关联渲染器 +//创建窗口和与之关联的渲染器 SDL_Window* window = SDL_CreateWindow(u8"你好,世界 ", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED); diff --git a/Demo/test.csv b/Demo/test.csv new file mode 100644 index 0000000..e115855 --- /dev/null +++ b/Demo/test.csv @@ -0,0 +1,3 @@ +1 2 3 +4 5 6 +7 8 9 diff --git a/Demo/test.json b/Demo/test.json new file mode 100644 index 0000000..c91ce7d --- /dev/null +++ b/Demo/test.json @@ -0,0 +1,5 @@ +{ + "name": "XiaoMing", + "age": 18, + "pets": ["dog","cat","bird"] +} \ No newline at end of file