rapidjson: working code for reading document from file? - c++

I need a working c++ code for reading document from file using rapidjson: https://code.google.com/p/rapidjson/
In the wiki it's yet undocumented, the examples unserialize only from std::string, I haven't a deep knowledge of templates.
I serialized my document into a text file and this is the code I wrote, but it doesn't
compile:
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/writer.h" // for stringify JSON
#include "rapidjson/filestream.h" // wrapper of C stream for prettywriter as output
[...]
std::ifstream myfile ("c:\\statdata.txt");
rapidjson::Document document;
document.ParseStream<0>(myfile);
the compilation error state: error: 'Document' is not a member of 'rapidjson'
I'm using Qt 4.8.1 with mingw and rapidjson v 0.1 (I already try with upgraded v 0.11 but the error remain)

#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <fstream>
using namespace rapidjson;
using namespace std;
ifstream ifs("test.json");
IStreamWrapper isw(ifs);
Document d;
d.ParseStream(isw);
Please read docs in http://rapidjson.org/md_doc_stream.html .

The FileStream in #Raanan's answer is apparently deprecated. There's a comment in the source code that says to use FileReadStream instead.
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
using namespace rapidjson;
// ...
FILE* pFile = fopen(fileName.c_str(), "rb");
char buffer[65536];
FileReadStream is(pFile, buffer, sizeof(buffer));
Document document;
document.ParseStream<0, UTF8<>, FileReadStream>(is);

Just found this question after having a rather similar problem. The solution would be to use a FILE* object, and not an ifstream together with rapidjson's own FileStream object (you already include the right header)
FILE * pFile = fopen ("test.json" , "r");
rapidjson::FileStream is(pFile);
rapidjson::Document document;
document.ParseStream<0>(is);
You of course need to add the document.h include (this answers your direct question, but will not solve the problem in your case, since you are using the wrong file stream):
#include "rapidjson/document.h"
The document object is then (rather fast i might add) filled with the file content. Hope it helps!

Related

How to read a file as a binary code in Qt?

I need to read a file as a binary code with Qt tools. In std c++ I did it like that:
#include <iostream>
#include <vector>
#include <fstream>
int main() {
std::ifstream file("C:\\Users\\%username%\\Desktop\\programme.exe",
std::ios::binary);
std::vector<char> vec((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>())); // that variable has the binary code
file.close();
return 0;
}
Qt is written using standard c++ as well as using native calls depending on OS and architecture. Therefore normal c++file I/O still works and is portable, otherwise have a look at the QFile documentation here. You would want to have a look at the examples that mention QDataStream

Cast a variable of filtering_istream type to ifstream type?

I am using the filetering_istream type to save the information in a decompressed file while using 'boost/iostreams/filtering_stream.hpp'. But I want to cast it into the ifstream type. It there any way to do it? Great thanks!
The code is as follows:
#include <istream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main(){
std::ifstream file("test_data.dat.gz");
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
/* add code to convert filtering_istream 'in' into ifstream 'pfile' */
/* It seems that the following code returns a pointer NULL */
// std::ifstream* pfile = in.component<std::ifstream>(1);
return 0;
}
After trying boost::ref and boost::wrapper proposed by zett42, the ifstream really works. The only problem is that it doesn't give the phrases wanted.
In my text of .gz file, I wrote:
THIS IS A DATA FILE!
8 plus 8 is 16
But using the ifstream, I got:
is_open: 1
\213<\373Xtest_data.dat\361\360V"G\307G7OWE.\205\202\234\322b\205\314bC3.\327+>\314$
I am not sure what happened here, and can I do something to recover it?
From the reference of filtering_stream:
filtering_stream derives from std::basic_istream, std::basic_ostream
or std::basic_iostream, depending on its Mode parameter.
So no, you can't cast a filtering_stream directly to an ifstream because there is no inheritance relationship between the two.
What you can do instead, if your filter chain ends with a device that is an ifstream, you can grap that device by calling filtering_stream::component(). For streams this function returns a boost::iostreams::detail::mode_adapter (you can see the type by calling in.component_type(1)).
It's propably not a good idea to depend on an internal boost type (indicated by namespace "detail") which could change with next boost version, so one workaround is to use boost::reference_wrapper instead.
#include <iostream>
#include <istream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/core/ref.hpp>
int main(){
std::ifstream file("test_data.dat.gz");
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(boost::ref(file));
if( auto pfile = in.component<boost::reference_wrapper<std::ifstream>>( 1 ) )
{
std::ifstream& rfile = *pfile;
std::cout << "is_open: " << rfile.is_open() << "\n";
}
}

Using Poco::Net::StreamSocket socket to download file from internet?

I want to using Poco::Net::StreamSocket socket to download file from internet.
I need a source template.
Anyone help me!
This code worked for me:
#include "Poco/URIStreamOpener.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/FTPStreamFactory.h"
#include <memory>
#include <iostream>
using Poco::URIStreamOpener;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;
using Poco::Net::HTTPStreamFactory;
using Poco::Net::FTPStreamFactory;
int main(int argc, char** argv)
{
HTTPStreamFactory::registerFactory(); // Must register the HTTP factory to stream using HTTP
FTPStreamFactory::registerFactory(); // Must register the FTP factory to stream using FTP
string url = "http://somefile.mp3";
string filePath = "C:\\somefile.mp3";
// Create and open a file stream
std::ofstream fileStream;
fileStream.open(filePath, ios::out | ios::trunc | ios::binary);
// Create the URI from the URL to the file.
URI uri(url);
// Open the stream and copy the data to the file.
std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
StreamCopier::copyStream(*pStr.get(), fileStream);
fileStream.close();
}
A lot of the above came from the example code found here.
Have a look on page 11 of the Poco network slides, it should help to get you started.

Writing To a file using fstream will erase the file

I have a problem, I have a saved text file, I want to use fstream header file in C++ to write something to that file, but using ofstream will erase the whole file as soon as I run the compiled application, why and how to avoid it?
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream write("1.txt");
return 0;
}
You need to tell ofstream to append to the file.
std::ofstream write("1.txt",std::ios::app);
There are several other flags that control similar characteristics of the stream, read a book or reference for more information.

Run-time error reading a .gz file using boost::iostreams and zlib

I am trying to read a .gz file and print the text content on screen by using boost::iostreams. This is just a simple experiment to learn about this library, and I am using the "directors.list.gz" file from IMDb (ftp://ftp.fu-berlin.de/pub/misc/movies/database/) as my input file.
My code compiles, via MSVC-10, but the process aborts when executed. There's not much information from the error message except for the error code being R6010.
Can someone please point me a direction in terms of what may have caused this and how do I make this work?
This library looks pretty neat and I do hope to use it correctly. Thanks a lot for helping.
#include <fstream> // isalpha
#include <iostream> // EOF
#include <boost/iostreams/categories.hpp> // input_filter_tag
#include <boost/iostreams/operations.hpp> // get
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filter/zlib.hpp>
using namespace std;
namespace io = boost::iostreams;
int main()
{
if(true)
{
string infile_path = "c:\\Temp\\directors.list.gz";
ifstream infile(infile_path, ios_base::in | ios_base::binary);
io::filtering_streambuf<io::input> in; //filter
in.push(io::zlib_decompressor());
in.push(infile);
//output to cout
io::copy(in, cout);
}
return 0;
}
The gzip file format has an additional header around the zlib data, which zlib can't read.
So you want to use boost's gzip_decompressor instead of zlib_decompressor.
in.push(gzip_decompressor());
Note you'll need to include boost/iostreams/filter/gzip.h instead of boost/iostreams/filter/zlib.h.
Here's a working example of streaming a GZIP file:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
using namespace boost::iostreams;
int main()
{
std::ifstream file("hello.gz", std::ios_base::in | std::ios_base::binary);
filtering_streambuf < input > in;
in.push(gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, std::cout);
}
You'll find more information on specific boost::iostreams filters lurking here in boost's documentation: http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/quick_reference.html#filters
I also feel I should point out that your code didn't compile with gcc: in the C++ standard library, the ifstream constructor takes a const char *, not a std::string. (I'm not sure about Microsoft's version).