I want to use liblas to treat some data so I installed the library on my ubuntu 13.04 at /usr/share/include , the automatic path.
I have also a project on code::blocks to use this library.
here is an extract of the main program :
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <liblas.hpp>
using namespace std;
int main()
{
std::ifstream ifs;
ifs.open("myfile.LAS", std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
std::cout << "Compressed: " << (header.Compressed() == true) ? "true":"false";
std::cout << "Signature: " << header.GetFileSignature() << '\n';
std::cout << "Points count: " << header.GetPointRecordsCount() << '\n';
return 0;
}
When I want to compile it, I got the error "liblas/version.hpp : no such file or directory"
Code::Blocks opens liblas.hpp and shows the error :
$#include liblas/version.hpp$
(under <>)
but in fact, there is in the same folder as liblas.hpp a file called version.hpp
What's wrong?
Related
I'm trying to figure out how to write to a file outside the working directory. This is the code I currently have.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string sp{};
std::fstream ss("C:\\Users\\onion\\AppData\\Roaming\\MetaQuotes\\Terminal\\some numbers\\MQL5\\Files\\testnew.txt", std::ios::in | std::ios::out);
if (!ss.is_open()) std::cout << "Failed" << '\n';
else
{
while (ss.is_open())
{
std::getline(ss, sp);
std::cout << sp << '\n';
ss << "new data";
if (ss.eof())break;
}
}
}
I can read the file perfectly fine, but I cant write to it? Could it be that Metatrader itself is limiting my ability to write to a file or does a file have to be in the working directory to be able to write to it? or am I just doing it wrong?
Actually I am trying to parse a json file using rapidjson library . But when i am trying to add this header file in my code it shows me an error like this
"[Error] rapidjson/document.h: No such file or directory" and
"recipe for target 'main_1.o' failed"
here main_1 is my file name.
This is my actual code
#include<stdio.h>
#include "rapidjson/document.h"
using namespace rapidjson;
Document document;
document.Parse(json);
int main()
{
char name[50];
int t_value;
return 0;
}
And also i haven't idea about where i want to add my json file?
But i really don't know where i did a mistake? please anyone help me.
Kindly check the below link for installation of json, you can also visit rapidjson official website
RapidJson installation
And for your code:
Download all header files of rapidjson and keep it inside your current folder under rapidjson folder(new folder)
Write the below code inside main, compiler error will occur due to this.
Document document;
document.Parse(json);
If you are using Ubuntu then package manager can be used to install the rapidjson lib
$ sudo apt-get update
$ sudo apt-get install rapidjson-dev
The path of the rapidjson include for me was
/usr/include/rapidjson
and in the cpp/hpp file
#include <rapidjson/document.h>
worked for me
sample program to load file
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/ostreamwrapper.h>
int main()
{
using namespace rapidjson;
std::ifstream ifs { R"(myfile.json)" };
if ( !ifs.is_open() )
{
std::cerr << "Could not open file for reading!\n";
return EXIT_FAILURE;
}
IStreamWrapper isw { ifs };
Document doc {};
doc.ParseStream( isw );
StringBuffer buffer {};
Writer<StringBuffer> writer { buffer };
doc.Accept( writer );
if ( doc.HasParseError() )
{
std::cout << "Error : " << doc.GetParseError() << '\n'
<< "Offset : " << doc.GetErrorOffset() << '\n';
return EXIT_FAILURE;
}
const std::string jsonStr { buffer.GetString() };
std::cout << jsonStr << '\n';
std::cout <<"done\n";
return EXIT_SUCCESS;
}
Demo code Source:
How to read json file using rapidjson and output to std::string?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string txt="";
ifstream file;
file.open ("ernio.txt", ios::in);
if (file.is_open()) {
while (getline(file, txt)) {
cout << txt << endl;
}
}
else
cout << "example" << endl;
return 0;
}
It prints example instead of reading line by line from the file. What am I doing wrong?!? (the file is in the exact same place as the main.cpp) We even tried:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string txt="";
ifstream file("ernio.txt");
if (file.is_open()) {
while (getline(file, txt)) {
cout << txt << endl;
}
}
else
cout << "example" << endl;
return 0;
}
Please help
The file needs to be in the directory from where the executable will be called, not in the source directory where your main.cpp resides.
When you build small programs with gcc or something similar from the command line, often the executable is in the current working directory, where the compiler will also draw the source files from.
When using a build system or an IDE, however, then usually the target of a build is different from that where the sources reside.
My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...
Description:
I am trying to move all the files in a directory to a certain(user choosen directory)based on their extension to a certain directory via boost file system.
Problem:
When the rename/copy_file method of boost filesystem gets hit,I am receiving the R6010-Abort method called error.
Example:
SourceDirectory:C:\Source\a.txt
DestinationDirectory:C:\Destination
After execution:
DestinationDirectory:C:\Destination\a.txt
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include "boost/algorithm/string/regex.hpp"
#include "boost/regex.hpp"
namespace fs = boost::filesystem;
using namespace std;
void categorizeFolder()
{
//Source Folder
std::string folderToCategorize;
cout<<"Choose the folder you want to categorize:";
cin>>folderToCategorize;
cout << "The directory you have choosen is: " << folderToCategorize << endl;
//Destination folder
std::string newfolder;
cout<<"Choose the folder you want to store your files:";
cin>>newfolder;
cout << "The directory you have choosen is: " << newfolder << endl;
std::vector< std::string > all_matching_files;
boost::filesystem::directory_iterator end_itr;
for( boost::filesystem::directory_iterator i( folderToCategorize ); i != end_itr; ++i )
{
if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
if( i->path().extension() == ".txt" )
{
cout<<i->path().extension();//Printing File extension
cout<<i->path();//Printing file path
cout<<i->path().filename()<<endl; //Printing filename
fs::rename(i->path(), newfolder);//This would move the file//Even tried fs::copy_file(i->path(), newfolder)
}
}
}
Kindly let me know if i am missing something in the above code.Thanks in advance.
Regards,
Ravi
The linux error looks like this:
terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
what(): boost::filesystem::rename: Is a directory: "/tmp/first/test.txt", "/tmp/second"
The fact that the API call is named rename and not, e.g. moveToFolder, should have given you an idea that you need to supply a full pathname in the target.
fs::rename(
it->path(),
fs::path(newfolder) / it->path().filename());
to fix it.
Here's a version with some better organization and error handling. It will even create the target directory if it doesn't already exist!
#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
namespace fs = boost::filesystem;
using namespace std;
void categorizeFolder(fs::path folderToCategorize, fs::path newfolder)
{
if (!fs::exists(newfolder))
fs::create_directories(newfolder);
if (!fs::is_directory(newfolder))
{
std::cerr << "Destination folder does not exist and could not be created: " << fs::absolute(newfolder) << "\n";
return;
}
for(fs::directory_iterator it(folderToCategorize), end_itr; it != end_itr; ++it)
{
if(!fs::is_regular_file(it->status()))
continue;
if(it->path().extension() == ".txt")
{
// std::cout << it->path().extension() << "\n";
// std::cout << it->path() << "\n";
// std::cout << it->path().filename() << "\n";
fs::rename(it->path(), fs::path(newfolder) / it->path().filename()); // move the file
}
}
}
int main(int argc, const char *argv[])
{
if (argc<3)
{
std::cout << "Usage: " << argv[0] << " folderToCategorize newfolder\n";
return 255;
}
std::string const folderToCategorize = argv[1];
std::string const newfolder = argv[2];
std::cout << "The directory you have choosen is: " << folderToCategorize << endl;
std::cout << "The directory you have choosen is: " << newfolder << endl;
categorizeFolder(folderToCategorize, newfolder);
}