2

I want to read a json data from a file in C++ using boost libraries. Using property tree. I am new to programming, very new to c++ and its the first time i use boost library. I had some history in C many years ago. And i have a weeks experience in C++ using SFML library.

Below is my template code loads a file, reads data and if fails give an error. I want to change my error handling a bit different way. 1. if i cant open the mentioned file because it doesn't exist I want to create a blank file named accordingly. 2. But if some other error happens but the file exists I don't want to accidentally delete the file and create new one (erasing the data).

I guess it will be something like

catch (const std::exception& e)
{
 if (e.type == std::exception::filenotfound()) //whatever function i need
   {
    boost::property_tree::write_json("./data.json", pt);
   }
 else
   {
    std::cout << e.what() << std::endl;
   }
}

So I want to create the file only if it genuinely doesn't exist but if somehow corrupted, missing data i look for or some unimaginable other error happens I don't want to delete it. Here is my template (without the implementation of what i want)

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
int main()
{
    boost::property_tree::ptree pt;
    try
    {
        boost::property_tree::read_json("./data.json", pt);
    }
    catch (const std::exception& e)
    {
     std::cout << e.what() << std::endl;
    }
    std::cout << pt.get<std::string>("test_name","default") << std::endl;

    return 0;

}

How exactly I should write this code. I searched the internet for 2 hours but I couldn't find anything i want. (Or at least I didn't notice) And I am not experienced enough to decode original library documentations. They feel like encrypted to me so I look for samples instead.

Morgoth
  • 4,935
  • 8
  • 40
  • 66
Kaan Kara
  • 47
  • 1
  • 4
  • 1
    It seems like you're looking for [the `ptree_bad_path` exception](http://www.boost.org/doc/libs/1_63_0/doc/html/boost/property_tree/ptree_bad_path.html). You should have been able to find it pretty quickly if you [read the reference documentation](http://www.boost.org/doc/libs/1_63_0/doc/html/property_tree/reference.html). – Some programmer dude Feb 09 '17 at 08:50
  • Thank you. But for some reason i couldnt manage to implement it. Tried to test it in the following way: `try { boost::property_tree::read_json("./data.json", pt); } catch (const boost::property_tree::ptree_bad_path& e1) { std::cout << "I didnt create the file yet"; } catch (const std::exception& e2) { std::cout << e2.what() << std::endl; }` It still gave me the original error. How should i implement it ? – Kaan Kara Feb 09 '17 at 09:03
  • And what *is* the "original error"? – Some programmer dude Feb 09 '17 at 09:09
  • "./data.json cannot open file" was the original error. I am trying to test my implementation first by making it write something other than this so i will know i override it. – Kaan Kara Feb 09 '17 at 09:13
  • So that error message is printed? By Boost or by you? Is an exception thrown? Also it seems I was wrong with the exception, it's thrown for a bad path for the actual property tree, sorry for that. – Some programmer dude Feb 09 '17 at 09:14
  • This error message is printed by std library. (Second part of the blog) I didnt throw an exception inside the code but removed the file to simulate it. Anyway still thank you for the effort. – Kaan Kara Feb 09 '17 at 09:20
  • [Reading the source](http://www.boost.org/doc/libs/master/boost/property_tree/json_parser.hpp), failure to open the file throws an `json_parser_error` error with that information: Pathname and the string "cannot open file". So that's what you need to check for. – Some programmer dude Feb 09 '17 at 09:21
  • Thank you very much. It solved my problem. – Kaan Kara Feb 09 '17 at 09:27

1 Answers1

2

https://stackoverflow.com/users/440558/some-programmer-dude has answered my question. Here is the exact coding:

try
{
     // Trying to load the file
}
catch (const boost::property_tree::json_parser_error& e1)
{
    //Here what i do if i cant find the file
}

If file doesnt exist it does something. But if file exists but if its in wrong format or doesnt have the approriate data it doesnt do anything.

Community
  • 1
  • 1
Kaan Kara
  • 47
  • 1
  • 4