Is boost::property_tree::ptree can't handle files which use UTF-8 with BOM?
#include <boost/filesystem.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <cstdlib>
#include <iostream>
int main()
{
try
{
boost::filesystem::path path("helper.ini");
boost::property_tree::ptree pt;
boost::property_tree::read_ini(path.string(), pt);
const std::string foo = pt.get<std::string>("foo");
std::cout << foo << '\n';
}
catch (const boost::property_tree::ini_parser_error& e)
{
std::cerr << "An error occurred while reading config file: " << e.what() << '\n';
return EXIT_FAILURE;
}
catch (const boost::property_tree::ptree_bad_data& e)
{
std::cerr << "An error occurred while getting options from config file: " << e.what() << '\n';
return EXIT_FAILURE;
}
catch (const boost::property_tree::ptree_bad_path& e)
{
std::cerr << "An error occurred while getting options from config file: " << e.what() << '\n';
return EXIT_FAILURE;
}
catch (...)
{
std::cerr << "Unknown error \n";
return EXIT_FAILURE;
}
}
helper.ini
foo=str
Output
An error occurred while getting options from config file: No such node
(foo)
What can i do with it? Manually delete BOM from file bedore reading it?
boost 1.53
I'm using this to skip BOM characters:
boost::property_tree::ptree pt;
std::ifstream file("file.ini", std::ios::in);
if (file.is_open())
{
//skip BOM
unsigned char buffer[8];
buffer[0] = 255;
while (file.good() && buffer[0] > 127)
file.read((char *)buffer, 1);
std::fpos_t pos = file.tellg();
if (pos > 0)
file.seekg(pos - 1);
//parse rest stream
boost::property_tree::ini_parser::read_ini(file, pt);
file.close();
}
Yes, easiest option would be to just check if the file starts with BOM and remove it.
You can file a bug against boost (probably should)
You could use boost::iosteams filters to remove BOM from the input stream whenever its found:
filter usage
filter concept
input filter concept with example that can be adapted to drop BOM
Related
I read about substr from here
http://www.cplusplus.com/reference/string/string/substr/
Here is my code :
int main()
{
std::ifstream in ("c:\\users\\admin\\desktop\\aaa.txt");
std::ofstream out ("c:\\users\\admin\\desktop\\bbb.txt");
std::string s ;
while ( getline (in,s) )
{
std::size_t startpos = s.find("test");
std::string str = s.substr (startpos);
out << str << endl;
}
in.close();
out.close();
}
I get error : R6010 abort() has been called
Note : aaa.txt contains spaces/characters/html tags
Any idea ?
Since I dont know the content of the text file, could you try making the following changes and let me know if the error is still being shown:
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
ifstream in("example.txt");
ofstream out("bbb.txt");
string s = std::string();
string str = std::string();
while (getline(in, s))
{
size_t startpos = s.find("test");
cout << s;
if (startpos != std::string::npos){
str = s.substr(startpos);
out << str << endl;
}
}
in.close();
out.close();
getchar();
return 0;
}
I am using if (startpos != std::string::npos) condition to check what to do when the find succeeds, this is missing in your code. adding this case will resolve your error.
Keep coding :)
While Code Frenzy answer is right, you can also use exceptions to help catch these kind of errors:
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
std::ifstream in ("aaa.txt");
std::ofstream out ("bbb.txt");
std::string s ;
try
{
while ( getline (in,s) )
{
std::size_t startpos = s.find("test");
std::string str = s.substr (startpos);
out << str << endl;
}
in.close();
out.close();
}
catch(std::exception e)
{
// (1) it will catch the error show show
cerr << e.what() << endl;
}
catch(std::out_of_range e)
{
// (2) this will also catch the same error if (1) was not there but could
// get you more details if you wanted since its more specific but i have
// not digged into it further
cerr << e.what() << endl;
}
catch(...)
{
// (3) just for sanity check if first two didn't catch it
cerr << "something went wrong";
}
}
The exceptoin catches this error and prints the message:
invalid string position
string mapFile;
cout << "Enter the file name : ";
cin >> mapFile;
ifstream mapfh;
mapfh.open(mapFile.c_str());
if(mapfh.is_open()) { ... }
else //if board file did not open properly
{
throw;
}
mapfh.close();
I am compiling with g++ in the command line. Whenever I put a file input (even with a full path i.e. /User/...etc./file.txt) it throws an error. I know the input is good, but for whatever reason the open always fails.
This isn't fully portable, but you'll get a more informed output if you interpret the errno,
#include <cerrno>
#include <cstring>
...
if(mapfh.is_open()) { ... }
else //if board file did not open properly
{
std::cout << "error: " << strerror(errno) << std::endl;
throw;
}
And if your policy is to communicate the errors as exceptions then use iostreams native support for the exceptions:
ifstream mapfh;
mapfh.exceptions(std::ios::failbit);
try {
mapfh.open(mapFile.c_str());
...
mapfh.close();
} catch (const std::exception& e) {
std::cout << e.what() << " : " << std::strerror(errno) << std::endl;
}
Using GCC 4.7.3 on Cygwin 1.7.24. Compiler options include: -std=gnu++11 -Wall -Wextra
I am working on a command line application and I needed to be able to load and save a set of strings so I wrote a quick wrapper class around std::set to add load and save methods.
// KeySet.h
#ifndef KEYSET_H
#define KEYSET_H
#include <cstdlib>
#include <sys/stat.h>
#include <cerrno>
#include <cstring>
#include <string>
#include <set>
#include <iostream>
#include <fstream>
inline bool file_exists (const std::string& filename)
{
/*
Utility routine to check existance of a file. Returns true or false,
prints an error and exits with status 2 on an error.
*/
struct stat buffer;
int error = stat(filename.c_str(), &buffer);
if (error == 0) return true;
if (errno == ENOENT) return false;
std::cerr << "Error while checking for '" << filename << "': " << strerror(errno) << std::endl;
exit (2);
}
class KeySet
{
private:
std::string filename;
std::set<std::string> keys;
public:
KeySet() {}
KeySet(const std::string Pfilename) : filename(Pfilename) {}
void set_filename (const std::string Pfilename) {filename = Pfilename;}
std::string get_filename () {return filename;}
auto size () -> decltype(keys.size()) {return keys.size();}
auto cbegin() -> decltype(keys.cbegin()) {return keys.cbegin();}
auto cend() -> decltype(keys.cend()) {return keys.cend();}
auto insert(const std::string key) -> decltype(keys.insert(key)) {return keys.insert(key);}
void load ();
void save ();
};
void KeySet::load ()
{
if (file_exists(filename)) {
errno = 0;
std::ifstream in (filename, std::ios_base::in);
if (in.fail()) {
std::cerr << "Error opening '" << filename << "' for reading: " << strerror(errno) << std::endl;
exit (2);
}
std::string token;
if (token.capacity() < 32) token.reserve(32);
while (in >> token) keys.insert(token);
if (!in.eof()) {
std::cerr << "Error reading '" << filename << "': " << strerror(errno) << std::endl;
exit (2);
}
in.clear(); // need to clear flags before calling close
in.close();
if (in.fail()) {
std::cerr << "Error closing '" << filename << "': " << strerror(errno) << std::endl;
exit (2);
}
}
}
void KeySet::save ()
{
errno = 0;
std::ofstream out (filename, std::ios_base::out);
if (out.fail()) {
std::cerr << "Error opening '" << filename << "' for writing: " << strerror(errno) << std::endl;
exit (2);
}
for (auto key = keys.cbegin(), end = keys.cend(); key != end; ++key) {
out << *key << std::endl;
}
out.close();
if (out.fail()) {
std::cerr << "Error writing '" << filename << "': " << strerror(errno) << std::endl;
exit (2);
}
}
#endif
//
Here's a quick program to test the load method.
// ks_test.cpp
#include "KeySet.h"
int main()
{
KeySet test;
std::string filename = "foo.keys.txt";
test.set_filename(filename);
test.load();
for (auto key = test.cbegin(), end = test.cend(); key != end; ++key) {
std::cout << *key << std::endl;
}
}
The data file just has "one two three" in it.
When I go to run the test program, I get the following error from my test program:
$ ./ks_test
Error closing 'foo.keys.txt': No error
Both cppreference.com and cplusplus.com say that the close method should set the fail bit on error. The save method works fine, and the load method works correctly if I comment out the error check after the close. Should this really work or have I misunderstood how close is supposed to work? Thanks in advance.
Edited to clarify, fix typo's and adjust code per Joachim Pileborg's and Konrad Rudolph's comments.
Edited to add solution to the code.
You have two errors here: The first is about how you do your reading, more specifically the loop for reading. The eof flag will not be set until after you tried to read and the read failed. Instead you should do like this:
while (in >> token) { ... }
Otherwise you will loop one time to many and try to read beyond the end of the file.
The second problem is the one you notice, and it depends on the the first problem. Since you try to read beyond the end of the file, the stream will set failbit causing in.fail() to return true even though there is no real error.
As it turns out, the close method for ifstream (and I assume all other IO objects) DOES NOT clear the error flags before closing the file. This means you need to add an explicit clear() call before you close the stream after end of file if you are checking for errors during the close. In my case, I added in.clear(); just before the in.close(); call and it is working as I expect.
I have a small code with some file I/O
bool loadConfigFile(std::string configFileName)
{
std::ifstream configFile;
try
{
configFile.open(configFileName, std::ifstream::in);
if(true != configFile.good())
{
throw std::exception("Problem with config file");
}
} catch (std::exception &e)
{
fprintf(stderr, "There was an error while opening the file: %s\n %s\n" , configFileName, e.what());
configFile.close();
}
configFile.close();
return true;
}
And everytime I launch the program without the file provided as a parameter I get some rubbish on output (random characters) or an unexpected error in runtime. What am I doing wrong here ?
"%s" expects an null terminated char array as its input but the code is passing configFileName, which is a std::string. Either use std::string::.c_str() or use std::cerr instead:
std::cerr << "There was an error while opening the file: "
<< configFileName << '\n'
<< e.what() << '\n';
Note that the ifstream constructor has a variant that accepts the filename to open and the destructor will close the stream if it is open so the explicit calls to open() and close() can be omitted:
try
{
std::ifstream configFile(configFileName);
if (!configFile.is_open())
{
throw std::exception("Failed to open '" + configFileName + "'");
}
}
catch (const std::exception& e)
{
// Handle exception.
std::cerr << e.what() << '\n';
return false;
}
I want to uncompress a file and write its content into a stringstream.
This is the code I tried:
string readGZipLog () {
try {
using namespace boost::iostreams;
ifstream file(currentFile.c_str(), std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(gzip_decompressor());
in.push(file);
std::stringstream strstream;
boost::iostreams::copy(in, strstream);
return strstream.str();
} catch (std::exception& e) {
cout << e.what() << endl;
}
}
void writeGZipLog (char* data) {
try {
using namespace boost::iostreams;
std::ofstream file( currentFile.c_str(), std::ios_base::out | std::ios_base::binary );
boost::iostreams::filtering_ostream out;
out.push( gzip_compressor() );
out.push(file);
std::stringstream strstream;
strstream << data;
boost::iostreams::copy( strstream, data );
} catch (std::exception& e) {
cout << e.what() << endl;
}
}
It compiles without any warnings (and of course errors) but the function readGZipLog() crashes while running:
gzip error
./build: line 3: 22174 Segmentation fault ./test
./build is the script that compiles and starts the application ./test automatically
I checked the file: It contains something, but I can't ungzip it using gunzip. So I am not sure whether the compression worked properly and if this has something to do with the gzip error thrown by Boost.
Can you give me a hit where the error(s) is(/are)?
Thanks for your help!
Paul
after a lot of research and trying I finally found a way how to handle (de)compression correctly.
This is the code that works for me without any problems (with gzip and bzip2):
string readGZipLog () {
using namespace boost::iostreams;
using namespace std;
try {
ifstream file(currentFile.c_str(), ios_base::in | ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(gzip_decompressor());
in.push(file);
stringstream strstream;
boost::iostreams::copy(in, strstream);
return strstream.str();
} catch (const gzip_error& exception) {
cout << "Boost Description of Error: " << exception.what() << endl;
return "err";
}
}
bool writeGZipLog (char* data) {
using namespace boost::iostreams;
using namespace std;
try {
std::ofstream file( currentFile.c_str(), std::ios_base::app );
boost::iostreams::filtering_ostream out;
out.push( gzip_compressor() );
out.push(file);
stringstream strstream;
strstream << data;
boost::iostreams::copy(strstream, out);
return true;
} catch (const gzip_error& exception) {
cout << "Boost Description of Error: " << exception.what() << endl;
return false;
}
}
What I can say is that I did some errors that were not necessary and I just found by looking at the code again many hours later. boost::iostreams::copy( std::stringstream , char* ); for example will even fail if 1 + 1 was 3.
I hope that this code piece will help somebody as it helped me.
Paul :)