提高带有多个“."的文件扩展名. [英] boost filename extension with multiple "."
问题描述
我有这样的文件名:09.04.201115_09_ch_4.txt
我只想提取文件.我尝试使用boost文件系统路径,但是文件名中的多个点存在问题. (我没有提出命名).
I would like to extract the filestem only. I tried using boost filesystem path, but it has problems with the multiple dots in the filename. (I didn't come up with the naming).
有没有办法解决?我可以只获取不带扩展名的文件名吗?我的代码如下:
Is there a way around that? Can I get only the filename without the extension? My code looks like this:
std::string stringtmp = path.parent_path().string() +"/"+path.stem().string()+".png" ;
好,这是代码(它使用的是ROOT框架):
Ok here's the code (it's using the ROOT framework):
#include "TH1F.h"
#include "TH2F.h"
#include "TF1.h"
#include "TSpectrum.h"
#include "TCanvas.h"
#include "TVirtualFitter.h"
#include "TMath.h"
#include "TGraph.h"
#include <fstream>
#include <iostream>
#include "TApplication.h"
#include "TImage.h"
#include <string>
#include <sstream>
#include "TStyle.h"
#include "TROOT.h"
#include "TGraph2D.h"
#include "boost/filesystem.hpp"
Int_t npeaks = 10;
Double_t fpeaks(Double_t *x, Double_t *par) {
Double_t result = par[0] + par[1]*x[0];
for (Int_t p=0;p<npeaks;p++) {
Double_t norm = par[3*p+2];
Double_t mean = par[3*p+3];
Double_t sigma = par[3*p+4];
result += norm*TMath::Gaus(x[0],mean,sigma);
}
return result;
}
void graph(std::string name, bool big){
Float_t x[5001],y[5001];
std::ifstream in;
TCanvas *c1 = new TCanvas("c1","c1",10,10,1000,500);
if(!big){
in.open(name.c_str());
for(Int_t i = 0 ; i< 5001 ;i++){
in >> x[i] >> y[i];
}
c1->cd(1);
TGraph *gr = new TGraph(5001,x,y);
gr->SetMinimum(-60.);
//gr->GetYAxis()->SetMinimum(-70.);
// gr->GetYAxis()->SetTitle("dB");
gr->Draw("A*");
TH1F *h = gr->GetHistogram();
for(Int_t i = 0 ; i< 5001 ;i++){
if(y[i]>= -60.)
h->SetBinContent(i,y[i]);
}
//c1->cd(2);
h->SetYTitle("Intensity in dB");
h->SetXTitle("#lambda in nm");
h->SetTitle("Sectrum");
h->Draw();
TSpectrum *s = new TSpectrum(1000);
Int_t nfound = s->Search(h,1,"new");
std::cout <<"Found " << nfound << " candiate peaks to fit\n";
c1->Update();
//estimate linear background
Double_t par[3000];
TF1 *fline = new TF1("fline","pol1",842,852);
h->Fit("fline","qn");
//c1->cd(2);
par[0] = fline->GetParameter(0);
par[1] = fline->GetParameter(1);
//loop on all found peaks. Eliminate peaks at the background level
Float_t *xpeaks = s->GetPositionX();
for (Int_t p=0;p<nfound;p++) {
Float_t xp = xpeaks[p];
Int_t bin = h->GetXaxis()->FindBin(xp);
Float_t yp = h->GetBinContent(bin);
if (yp-TMath::Sqrt(yp) < fline->Eval(xp)) continue;
par[3*npeaks+2] = yp;
par[3*npeaks+3] = xp;
par[3*npeaks+4] = 3;
npeaks++;
}
c1->Update();
TImage *img = TImage::Create();
img->FromPad(c1);
std::stringstream _name;
_name << name << ".png";
_name >> name;
boost::filesystem::path path(name);
std::string stringtmp = path.parent_path().string() +"/"+path.stem().string()+".png" ;
std::cout <<"\n \n stem \n \n"<<stringtmp<< '\t' << path.stem().string() << std::endl;
img->WriteImage(stringtmp.c_str());
return;
}
输出看起来像这样:
警告:删除具有相同名称的画布:c1
Warning in : Deleting canvas with same name: c1
找到了39个适合的候选峰
Found 39 candiate peaks to fit
stem
29/12.04.201115_09_ch_4.txt.png 12.04.201115_09_ch_4.txt
推荐答案
这对我有用:
#include <ostream>
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path const p("C:\\Code\\09.04.201115_09_ch_4.txt");
// prints "C:\Code\09.04.201115_09_ch_4.txt"
std::cout << p << '\n';
// prints "09.04.201115_09_ch_4.txt"
std::cout << p.filename() << '\n';
// prints "09.04.201115_09_ch_4"
std::cout << p.stem() << std::endl;
}
因此,基本上您需要的只是p.stem()
;如果这对您不起作用,则需要发布可编译的副本.
So, essentially all you need is p.stem()
; if that doesn't work for you, you'll need to post a compilable repro.
这篇关于提高带有多个“."的文件扩展名.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!