我有这样的文件名:09.04.201115_09_ch_4.txt
我想只提取filestem。我尝试使用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 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
发现39个念珠峰适合
stem
29/12.04.201115_09_ch_4.txt.png 12.04.201115_09_ch_4.txt
答案 0 :(得分:8)
这对我有用:
#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()
;如果这对你不起作用,你需要发布一个可编辑的复制品。