I need to load a JSON file and copy it into a string buffer but it is failing to open the file.
What could be the problem, can I use any other method?
My JSON file is as follows:
{
"ip": [ "212.253.144.10","192.32.12.1","192.12.1.1"]
}
and the code:
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
void CMFCJasonDlg::OnBnClickedButton2()
{
ifstream inputFile("C:\Users\hp\Desktop\VK\ip.json");
if (!inputFile.is_open())
{
MessageBox(L"Failed to open file", 0, 0);
CloseHandle(hdevice);
return;
}
stringstream buffer;
buffer << inputFile.rdbuf();
string inputString = buffer.str();
inputFile.close();
DWORD inputBufferSize = sizeof(inputString);
char* inputBuffer = new char[inputBufferSize];
strncpy_s(inputBuffer, inputBufferSize, inputString.c_str(), inputString.size());
delete[] inputBuffer;
}
2
Answers
Try it with
The +1 is for the null terminator
Your code has two problem:
First,
sizeof(inputString)
returns the size of the variableinputString
, which has nothing to do with its length. You should useinputString.length()
to obtain the length.Second, every string stored in a byte array is terminated by a null terminator (