#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main ()
{
if (std::regex_match ("http://www.google.com", std::regex("(http|https):\/\/(\w+\.)*(\w*)\/([\w\d]+\/{0,1})+")))
std::cout << "valid URL \n";
std::cout << std::endl;
return 0;
}
its compiling with the warnings ,but when I executed it gives
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)
what I should Do?
The warnings that you are ignoring are probably telling you what the problem is.
By looking at the pattern, you have not properly escaped the pattern string.
Properly escaping the pattern string to use '\' to escape the backslash will solve the problem. Otherwise, the compiler is tring to interpret the character that follows the un-escaped backslash into a string control character.
std::regex("(http|https)://(\\w+.)(\\w)/([\\w\\d]+/{0,1})+")
Try cpp-netlib:
#include <string>
#include <iostream>
#include <boost/network/uri.hpp>
int main (int argc, char ** argv)
{
std::string address = "http://www.google.com";
boost::network::uri::uri uri_(address);
if ( !boost::network::uri::valid(uri_) )
{
// error
std::cout << "not valid" << std::endl;
return 0;
}
std::cout << "valid" << std::endl;
std::string host = boost::network::uri::host(uri_);
std::string port = boost::network::uri::port(uri_);
std::string scheme = boost::network::uri::scheme(uri_);
return 0;
}
How to build (cpp-netlib is in /root/cpp-netlib-0.9.4/ in my case):
g++ main.cpp -L/root/cpp-netlib-0.9.4/libs/network/src/ -I/root/cpp-netlib-0.9.4/ -o main -lcppnetlib-uri -lboost_system
Related
I am trying to reference a file through using it as a command line argument, assuming the file is in the same directory. I then need to convert the first argument into a string. Here is my code :
#include <iostream>
#include <fstream>
#include "header.hpp"
using namespace std;
int main(int argc, char *argv[]) {
string arg1 = argv[1];
string arg2 = argv[2];
string fLine;
ifstream input(arg1);
if (input.is_open()) {
while ( getline (input, fLine)) {
cout << fLine << endl;
}
input.close();
}
return 0;
}
It compiles fine but when I try execute it the error is
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
From the constructor documentation for std::string:
from c-string (4) string (const char* s);
If s is a null pointer, if n == npos, or if the range specified by [first,last) is not valid, it causes undefined behavior.
Your implementation is choosing to throw an exception here when passed a nullptr value for const char* s.
As noted above, it is likely that string arg2 = argv[2]; is generating this error (and it is possible that accessing argv[2] itself is undefined, as you only mention a single argument being provided to the program.
In C++ array indices start with 0.
So, argv[0] will be the path/name of your program and argv[1] will be the name of the file that you want to work with.
argv[2] does most probably not exist. Will be nullptr and hence the error message.
You should always check argc, to see, how many arguments have been passed. But beware. argv[0] is always the name/path of your program.
EDIT: Please try:
#include <iostream>
#include <fstream>
#include <string>
#include "header.hpp"
using namespace std;
int main(int argc, char* argv[]) {
string arg1 = argv[1];
string fLine;
ifstream input(arg1);
if (input) {
while (getline(input, fLine)) {
cout << fLine << endl;
}
input.close();
}
else std::cerr << "\n\n*** Error: cannot open " << arg1 << '\n';
return 0;
}
I'm trying to get started with Boost for C++. Here's a small program that compiles with g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a.
However, when I run it, here's the error I get:
libc++abi.dylib: terminating with uncaught exception of type std::bad_cast: std::bad_cast
Abort trap: 6
#include <string>
#include <iostream>
#include <boost/locale.hpp>
int main(void) {
char test[] = "Variété";
boost::locale::to_upper(test);
std::cout << test << std::endl;
return 0;
}
What could be the reason here? Thanks!
I'm on Mac OSX Mavericks.
According to docs:
http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/group__convert.html#ga7889a57e1bc1059fbb107db0781d0b6d
std::basic_string<CharType> boost::locale::to_lower(CharType const *str,
std::locale const &loc = std::locale())
Convert a NUL terminated string str to lower case according to locale loc
Note:
throws std::bad_cast if loc does not have converter facet installed
So, this fixes the problem on my machine.
#include <string>
#include <iostream>
#include <boost/locale.hpp>
int main(void) {
std::string test = "Variété";
std::locale loc = boost::locale::generator().generate("en_US.UTF-8");
std::string test_u = boost::locale::to_upper(test, loc);
std::cout << test << " -> " << test_u << std::endl;
return 0;
}
Outputs:
Variété -> VARIÉTÉ
I have simple code which doesn't work. It throws this error: what(): regex_error and I have no idea why. Thanks for any help. I tried to compile it with this flag: -std=c++11.
#include <iostream>
#include <regex>
int main() {
std::regex r("[1-9]{4}");
std::cout << "result: " << std::regex_match("1524", r) << '\n';
return 0;
}
I have a simple program running on Linux using g++ compiler:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv){
fstream file;
string s;
file.open("sample/dates.dat", fstream::in);
if(!file.good())
return 0;
getline(file, s);
cout << s << "." << endl;
return 0;
}
Compiled with: g++ -o test test.cpp. When I run this, the fullstop is printed BEFORE the string s, not after. Does anybody know why this is happening? And is it easy to fix?
Thanks.
If there is a carriage return at the end of the string it will move the position of output to the beginning of the console line when printed.
#include <iostream>
int main()
{
std::cout << "some line\r" << "." << std::endl;
// ^^ carriage return
}
I am having trouble extracting the token values from my string : "JOIN #ROOM\r\n"
I am compiling my code on Mingw64 with the following arguments : g++ tregex.cpp -o tregex.exe -std=gnu++11
I get this error , but not my exception for some reason :
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
This is my code :
#include <regex>
#include <string>
#include <iostream>
using namespace std;
//Tregex.cpp
int main(void) {
regex rgx("[[:cntrl:]]");
string str = "JOIN #ROOM\r\n";
smatch match;
try{
if(regex_search(str, match, rgx))
for(auto token:match) cout << token <<"\n";
cout<< endl;
}
catch(regex_error & e){
if( e.code() == regex_constants::error_escape )
cerr << "invalid escape character \n";
else if( e.code() == regex_constants::error_stack )
cerr << "regular expression is not big enough\n";
else
cerr << "exception caught: "<< e.what()<<"\n";
}
cin.get();
return 0;
}
FWIW, there's nothing wrong with your C++ code, and it works perfectly using Clang and libc++ on my MacBook.
As indicated by the comments above, <regex> is one of the features that works in the LLVM project's libc++ but has never worked properly in the GNU project's libstdc++. You might try switching to libc++ if it's available for your platform.