我需要模仿以下Perl代码的功能
if($file =~ /^([^_]+)_([^_]+)_MA_(\d{4})(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)\.MA$/) {
my ($radar, $beam, $year, $month, $day, $hour, $min, $sec) =
($1, $2, $3, $4, $5, $6, $7, $8);
my $file_ts = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
$year, $month, $day, $hour, $min, $sec);
# ...
}
在C ++中。我可以使用哪种功能?
答案 0 :(得分:9)
嗯,总是可以选择使用sprintf
:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
或者您可以使用stringstream
作为更多C ++ - 替代方案。
答案 1 :(得分:4)
与Perl相同:
char file_ts[1000];
snprintf(file_ts, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);