Sponsored Content
Full Discussion: Check for a substring
Top Forums Programming Check for a substring Post 302457283 by faizlo on Monday 27th of September 2010 04:11:30 PM
Old 09-27-2010
Corona688

strstr() would check to see if the substring str1 is part of string str2, as I read in the man pages.
so, to use strstr() you need to know str2.
In my case, str2 is a list of hundreds of files that I loop over one by one.
All I need is to skip the string if it has any of the 2 substrings I mentioned above.

Here is the code:
Code:
#include <string>
#include "TFile.h"
#include <iostream>
#include "TH1.h"
#include "TCanvas.h"
#include <fstream>

void func2()
{
  using std::cin;
  using std::cout;
  using std::endl;

// new data
//TFile *_file_0 = TFile::Open("m-00.root");
TFile *_file_0 = TFile::Open("m-00.root");

// old data
//TFile *_file_1 = TFile::Open("m-00.root");
TFile *_file_1 = TFile::Open("m-00.root");

 char var[20];
 string mystr = "pre_ti_data_";
 string myc = "MyC";

 ifstream myfile;
 myfile.open("vars.txt");
 if (myfile.is_open())
   {
     while (!myfile.eof())
       {
	 myfile >> var;
         string dutt = mystr+var+";1";
         string sun1 = mystr+var+".eps";
         string sun2 = mystr+var+".jpg";
	 cout << "What is dutt? " << dutt << endl;

	 //Pass bdt and nn
	 //
	 //if (dutt == "pre_ti_data_bdt" || dutt == "pre_ti_data_nn")
	 //  continue;  ????
         //

	 //TCanvas *MyC = new TCanvas(dutt.c_str(), "", 1);

	 TH1* h0  = (TH1*) _file_0->Get(dutt.c_str());
	 TH1* h1  = (TH1*) _file_1->Get(dutt.c_str());

	 TH1F* rat_d = (TH1F*) h0->Clone();
	 rat_d->Reset();
	 rat_d->Divide(h0, h1);
	 rat_d->SetMaximum(0.5);
	 rat_d->SetMinimum(0.0);
	 
	 //
	 //*****************************************
	 //this is for fitting the ratios histogram:
	 //*****************************************
	 cout << "Fitting: " << dutt << "ratio" << endl;
	 //rat_d->Fit("pol1");
	 rat_d->Fit("pol0");  //fitting with a constant function
	 gStyle->SetOptFit(111);
	 //
	 
         TCanvas *MyC = new TCanvas(myc.c_str(),"",1);
         MyC->Divide(2,1);
         MyC->cd(1);
         rat_d->Draw();

	 MyC->cd(2);
	 h0->SetLineColor(2);
	 Double_t n0 = h0->Integral();
	 cout << "Number of events in h0: " << n0 << endl;

	 //
	 //To make sure I have no zeros on the denominator
	 if (n0 == 0) {
	   cout << "     >>>>>>>>>>>>>>>>>      " << endl;
	   cout << "  I got a zero in the denominator" << endl;
	   cout << "     >>>>>>>>>>>>>>>>>      " << endl;
	   continue;
	 }
	 //continue after passing the zero:
	 //

	 h0->Scale(1/n0); //h0->Scale(1/h0->Integral())

	 h1->SetLineColor(4);
	 Double_t n1 = h1->Integral();
	 cout << "Number of events in h1: " << n1 << endl;
	 h1->Scale(1/n1);

	 h0->Draw();
	 h1->Draw("same");
         cout << "sun1 : " << sun1 << '\n';
         cout << "sun2 : " << sun2 << '\n';
	 MyC->Print(sun1.c_str());
	 MyC->Print(sun2.c_str());
continue;
       }
     myfile.close();
   }

Please notice that it is not a 100% C++, but this is because the ROOT system has some differences. Yet, all C++ codes work with it.
TFile and other strange words are classes from the framework I use.

~faizlo
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting a substring

This is probably pretty simple butI'm not sure how to best go about it. If I have FILE="myBigLongFileName_1.xls" FILE_PREFIX=`echo $FILE| cut -d"." -f1` # that gives "myBigLongFileName_1" All i want to do now is chop the "_1" from the end of $FILE_PREFIX Any ideas anyone? (3 Replies)
Discussion started by: djkane
3 Replies

2. Shell Programming and Scripting

how to get substring

i have a strings abc-def.csv ghi-jkl.csv i want to make it as abc-*-def.xyz ghi-*-jkl.xyz How to do it?. (5 Replies)
Discussion started by: senthilk615
5 Replies

3. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

4. UNIX for Dummies Questions & Answers

Substring

Hi I use the below cmd to get the list of files that are modified than <temp> file in the <path> diretory cmd:find <path> -name '*.zip' -type f -newer <temp> -print i am getting all the list of files that are new or modified, with abs path, i want to copy all of these files to a... (3 Replies)
Discussion started by: Naveen_5960
3 Replies

5. Shell Programming and Scripting

substring

I have a string '<Hi>abc</Hi>" How to print "abc" (6 Replies)
Discussion started by: sandy1028
6 Replies

6. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

7. Shell Programming and Scripting

Get the substring

Hi All, I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell. Thanks Damon (11 Replies)
Discussion started by: Damon_Qu
11 Replies

8. Programming

check substring

hi everyone I have a C program where I have a line and I want to check if the line contains a string.The line is stored in a buffer.How can I do that? Can I consider the whole line as a string and check for a substring?And if so what's the most efficient way to achieve it? (1 Reply)
Discussion started by: vlm
1 Replies

9. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

10. UNIX for Beginners Questions & Answers

Check if string contains substring surrounded by numbers

Hi, I have a process that generates strings. I would like to check each string and search for substring which contains the letter 'E' surrounded by numbers (both sides of the letter 'E'). few examples: AA4E7012A2 - contains E surrounded by numbers FE18274012 - does not contain E... (3 Replies)
Discussion started by: yanive
3 Replies
FBB::LocalClientSocket(3bobcat) 			     Unix Domain client Socket				   FBB::LocalClientSocket(3bobcat)

NAME
FBB::LocalClientSocket - Client Socket connecting to a Server in the Unix Domain SYNOPSIS
#include <bobcat/localclientsocket> Linking option: -lbobcat DESCRIPTION
An FBB::LocalClientSocket may be constructed to connect to a server process in the Unix Domain. The socket made available by the FBB:Local- ClientSocket may be used to initialize a std::istream and/or std::ostream. The std::istream is used to read information from the server process to which the FBB::LocalClientSocket connects, The std::ostream is used to send information to the server process to which the FBB::LocalClientSocket connects. Since a socket may be considered a file descriptor the avaiable FBB::IFdStream, FBB::IFdStreamBuf, FBB::OFdStream, and FBB::OFdStreamBuf classes may be used profitably here. Note that having available a socket does not mean that this defines the communication protocol. It is (still) the responsibility of the programmer to comply with an existing protocol or to implement a tailor-made protocol. The latter situation implies that the sequence of input- and output operations is defined by the programmer. NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. INHERITS FROM
FBB::LocalSocketBase CONSTRUCTOR
o LocalClientSocket(): This constructor merely creates a FBB::LocalClientSocket object. Before it can be used, its open() member must be called. o LocalClientSocket(std::string const &name): This constructor initializes an FBB::LocalClientSocket object, using the named Unix Domain socket to connect to the server using the named Unix Domain socket. An FBB::Errno is thrown if the socket could not be constructed. The construction of the socket does not mean that a connection has actually been established. In order to connect to the server, the member connect() (see below) should be used. The copy constructor is not available. MEMBER FUNCTION
o int connect(): This member returns a socket that can be used to communicate with the server process. An FBB::Errno exception is thrown if the con- nection could not be established. o open(std::string const &name): This member function prepares the FBB::LocalClientSocket object, constructed earlier using the default constructor, for use. The named Unix Domain socket is used to connect to the server using the named Unix Domain socket. An FBB::Errno is thrown if the socket could not be constructed. The construction of the socket does not mean that a connection has actually been established. In order to connect to the server, the member connect() should be used. EXAMPLE
See also the localserversocket(3bobcat) example. #include <iostream> #include <bobcat/localclientsocket> #include <bobcat/ifdstream> #include <bobcat/ofdstream> using namespace std; using namespace FBB; int main(int argc, char **argv) try { if (argc == 1) { cerr << "Provide filename representing the unix domain socket "; return 1; } LocalClientSocket client(argv[1]); int fd = client.connect(); string line; cout << "Connecting to socket " << fd << endl; IFdStream in(fd); // stream to read from OFdStream out(fd); // stream to write to while (true) { // Ask for a textline, stop if empty / none cout << "? "; if (!getline(cin, line) || line.length() == 0) return 0; cout << "Line read: " << line << endl; // Return the line to the server out << line.c_str() << endl; cout << "wrote line "; // Wait for a reply from the server getline(in, line); cout << "Answer: " << line << endl; } return 0; } catch (Errno const &err) { cerr << err.what() << " " << "Can't connect to " << argv[1] << ", port " << argv[2] << endl; return 1; } FILES
bobcat/localclientsocket - defines the class interface SEE ALSO
bobcat(7), ifdstream(3bobcat), ifdstreambuf(3bobcat), clientsocket(3bobcat), localserversocket(3bobcat), localsocketbase(3bobcat), ofd- stream(3bobcat), ofdstream(3bobcat) BUGS
None Reported. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::LocalClientSocket(3bobcat)
All times are GMT -4. The time now is 01:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy