title: Having trouble opening a json file in C++
body: <p>I am trying to open a json file that I will be working with in C++.  Code that I have used successfully before fails to open the file.  I am using Visual Studio 2017 on Windows 10 Pro with JSON for Modern C++ version 3.5.0.</p>
  
  <p>I have a very simple function, which is supposed to open a file as input to a json object.  It appears to open the file, but aborts when writing it to the json object. Originally the file to be opened was in another directory, but I moved it into the same directory as the executable while testing...but it didn't help.</p>
  
  <p>Here is the very short function that fails:</p>
  
  <pre><code>json baselineOpenAndRead(string fileName) //passed string used for filename 
  {
  
      json baseJObject;
      cout &lt;&lt; "we have a baseJObject" &lt;&lt; endl;
  
      //ifstream inFileJSON("test_file.json");  // Making this explicit made no difference
      ifstream inFileJSON;
      inFileJSON.open("test_file.json", ifstream::in); 
      cout &lt;&lt; "we have opened json inFileJSON" &lt;&lt; endl; // get here
  
  
      inFileJSON &gt;&gt; baseJObject;
      cout &lt;&lt; " Can direct inFileJSON into baseJObject" &lt;&lt; endl; //never get here; the app aborts.
      inFileJSON.close();
      return baseJObject;
  }
  </code></pre>
  
  <p>This seems basically identical to the example on the nlohmann site:</p>
  
  <pre><code>// read a JSON file
  std::ifstream i("file.json");
  json j;
  i &gt;&gt; j;
  </code></pre>
  
  <p>I just expected this to open the json file, load it into the object, and return the object.  Instead, it just quits.</p>
  
  <p>Thanks for any thoughts...i.e., what am I doing wrong?  (I'm going to ignore that it worked before...maybe I missed something).</p>
  
  <p>--Al</p>
  
  <p><strong>As requested, here is a minimal reproducible example, but it will require nlohmann's json.hpp in order to compile:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include "json.hpp"
  
  using json = nlohmann::json;
  using namespace std;
  
  string fileName;
  json baselineOpenAndRead(string);
  
  int main(int argC, char *argV[])
  {
      json baseJObject;
  
      if (argC != 2) // check to make sure proper number of arguments are given.
      {
          cout &lt;&lt; "\n\nFilename needed...";
          exit(1); // number of arguments is wrong - exit program
      }
      else
      {
          fileName = argV[1];
          baseJObject = baselineOpenAndRead(fileName); // opens and reads the Base Line JSON file
          cout &lt;&lt; "baseJObject returned" &lt;&lt; endl;
      }
      return 0;
  }
  
  json baselineOpenAndRead(string fileName) // 
  {
      cout &lt;&lt; "File name: " &lt;&lt; fileName &lt;&lt; endl;
      json baseJObject;
      cout &lt;&lt; "we have a baseJObject" &lt;&lt; endl;
  
      ifstream inFileJSON(fileName);
  
      if (inFileJSON.is_open())
      {
          cout &lt;&lt; "file open..." &lt;&lt; endl;
          if (nlohmann::json::accept(inFileJSON))
          {
              cout &lt;&lt; "valid json" &lt;&lt; endl;
              try { inFileJSON &gt;&gt; baseJObject; }
              catch (const std::exception &amp;e) { std::cout &lt;&lt; e.what() &lt;&lt; '\n'; throw; }
          }
          else
          {
              cout &lt;&lt; "not valid json" &lt;&lt; endl;
          }
      }
      else
      {
          cout &lt;&lt; "file not really open" &lt;&lt; endl;
      }
      inFileJSON &gt;&gt; baseJObject;
      cout &lt;&lt; " We can echo inFileJSON into baseJObject" &lt;&lt; endl;
      inFileJSON.close();
      return baseJObject;
  }
  </code></pre>
  
  <p>I tested it with this json file:</p>
  
  <pre><code>{
      "people": [{
              "name": "Scott",
              "website": "stackabuse.com",
              "from": "Nebraska"
          },
          {
              "name": "Larry",
              "website": "google.com",
              "from": "Michigan"
          },
          {
              "name": "Tim",
              "website": "apple.com",
              "from": "Alabama"
          }
      ]
  }
  </code></pre>
  
  <p>When I run this passing it the json above as data.json, I get the following output and then it quits:</p>
  
  <pre><code>./Test_json data.json
  File name: data.json
  we have a baseJObject
  file open...
  valid json
  [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal
  </code></pre>
  
  <p>Without the try, it just quits.  It never gets past <strong>inFileJSON >> baseJObject;</strong></p>
  
  <p><strong>Another try that seems to work, but why?</strong></p>
  
  <p>OK.  I tried this with the same main (the only changes are in the function):</p>
  
  <pre><code>json baselineOpenAndRead(string fileName) // 
  {
  
      json baseJObject;
      string filePath = "../baselines/" + fileName;
      cout &lt;&lt; "filePath: " &lt;&lt; filePath &lt;&lt; endl;
      ifstream inFileJSON(fileName);
      //baseJObject = json::parse(inFileJSON);
      inFileJSON &gt;&gt; baseJObject;
      cout &lt;&lt; baseJObject &lt;&lt; std::endl;
      return baseJObject;
  }
  </code></pre>
  
  <p>This looks basically the same to me.  I tried making it ifstream inFileJSON(fileName.c_str()) on both the original and in this one.  The original continued to fail, this one continued to work.  Sorry this is getting so long, but I can't get decent formatting out of comments...  Should I just try answering my own question instead?</p>