LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2004, 02:47 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Rep: Reputation: 15
Question C....Search a string for a string


Does anybody know of a function or have a code example that searches a string, for contents of another string? For example, I would like C program to look at file names, and be able to search each "filename/string" for ".exe".

Thanks.

 
Old 06-12-2004, 02:57 PM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Code:
if (strstr(myfilename, ".exe")!=NULL)
{
    //Is an exe
}
strstr returns a pointer to the place in string1 where string2 was found.

Enjoy!
 
Old 06-12-2004, 03:21 PM   #3
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Thanks!!
 
Old 06-13-2004, 09:52 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
If you want to make sure that ".exe" are the last 4 characters in the filename, then this approach might be better:

Code:
if(strlen(filename) > 4)
  if(strcmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }
 
Old 06-14-2004, 04:15 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Quote:
Originally posted by itsme86
If you want to make sure that ".exe" are the last 4 characters in the filename, then this approach might be better:

Code:
if(strlen(filename) > 4)
  if(strcmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }
Actually...

Since he is doing this for Windows (at least I think he is, why else .exe?), then this would probably be even better:

Code:
if(strlen(filename) > 4)
  if(stricmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search for a string in c alaios Programming 1 07-07-2005 04:50 PM
[c++] Search in a string hylke Programming 5 06-14-2004 11:20 AM
search for my string alaios Linux - General 6 05-08-2004 01:32 PM
string search in C h/w Programming 17 10-13-2003 06:26 PM
Search for a string TheOriginalH Linux - General 5 09-24-2003 10:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration