开发者

PHP code on C++

I would like to know how this code would look in C++:

<?php

$read=fread(fopen('cookie.txt', '开发者_JAVA百科r'), filesize('cookie.txt'));


$pattern = "/[a-f0-9]{32}/";
preg_match($pattern, $read, $matches);
echo $matches[0];

?>


Navaz makes it look like reading a file in C++ is hard or requires pointers. Neither is the case. In fact, the file can be read in one line:

std::ifstream in("cookie.txt");
string str(static_cast<stringstream const&>(stringstream() << in.rdbuf()).str());

(This may look daunting but it’s conceptually simple. We read the file into a string stream and copy the result into a string str).

The rest of the solution would be identical.


Use boost::regex. The following is an example which may not be exact equivalent to your PHP code.I don't know PHP, so test it extensively before you use it. :-)

#include <string>
#include <iostream>
#include <fstream>
#include <boost/regex.hpp>

std::ifstream file("cookie.txt");
std::stringstream input;
input << file.rdbuf();
file.close();

boost::regex pattern("/[a-f0-9]{32}/");
boost::match_results<std::string::const_iterator> results;
if(boost::regex_match(input.str(), results, pattern, boost::match_default |  boost::match_partial))
{
    //results contains the matches
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜