使用多个"."提升文件扩展名

boost filename extension with multiple "."

本文关键字:文件 扩展名      更新时间:2023-10-16

我有这样的文件名:09.04.201115_09_ch_4.txt

我只想提取文件。我尝试使用boost文件系统路径,但它在文件名中的多个点有问题。(这个名字不是我想出来的)。

有办法吗?我可以只得到没有扩展名的文件名吗?我的代码是这样的:

 std::string stringtmp = path.parent_path().string() +"/"+path.stem().string()+".png" ;

下面是代码(它使用了ROOT框架):

#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 fitn";
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;
} 

输出如下所示:

删除同名画布

找到39个候选峰来拟合

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:Code9.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();如果这对你不起作用,你需要发布一个可编译的版本。