I need to get the 3 letters right of a string, the extension. Is there a function in string.h that can do that??
I used Right and Left in Visual Basic.
Printable View
I need to get the 3 letters right of a string, the extension. Is there a function in string.h that can do that??
I used Right and Left in Visual Basic.
I don't believe there's a standard one, but it'd be easy enough to build one yourself.......
No (In ANSI C atleast). But you can write your own with very little ease using the standard string functions.
Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
>Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
It depends on the way you implement it. strlen() is one which is real helpful.
One way of implementing is, extract all the characters individually from the string in the range: [strlen(string) - 4] to [strlen(string)]
Just remember to account for strings that don't have enough characters (ie empty ones)........
Well if you start with
char filename[] = "wibble.txt";
Then you can locate the dot with
char *p = strchr( filename, '.' );
Or locate the .txt with
char *p = strstr( filename, ".txt" );
In both cases, p either points to the dot, or p is NULL indicating that nothing was found.
Oh thank you Salem, it worked :) strchr was the solution i needed!
Unless you're using DOS (or potentially some other OS), you don't know for sure that there is only one "." in the file name.
Example: In *nix, it's common to have:
".tar.gz" on the end of the file.
One way is simply to loop through the string from the end. (Basicly strchr does the same thing, just from the beginning.)
Now, you could use strchr. Just use it with more than one character. Or, even better, use it in a loop:Code:for( x = strlen( s ) -1; x > -1; x-- )
{
if( s[x] == charToFind ) break;
}
That should do it.Code:c = strchr( string, tofind );
for( temp = c; temp != NULL; temp = strchr( c, tofind ) )
if( temp != NULL ) c = temp;
Quzah.
Correct me if I'm wrong, but wouldn't it be simpler to use strrchr().
For example:
Code:#include <stdio.h>
#include <string.h>
int main(void)
{
char x[] = "filename.txt";
char *p;
if ((p = strrchr(x, '.')) == NULL)
printf ("No extension\n");
else
printf ("Extension is %s\n", p+1);
return 0;
}
Sure. That'd work. :D However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions... (While it may be no big deal to the original poster, I'm generally pick regarding this.)
Quzah.
Yes they are ANSI functions
I need updated man pages then...Quote:
Originally posted by Salem
Yes they are ANSI functions
Quzah.
What 2 functions? :confused: I only used strrchr() and printf()? !! (I'm just being sarcastic, I guess you meant strchr() :D )Quote:
Originally posted by quzah
Sure. That'd work. :D However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions...
Also, if your documentation doesn't liste them, it's probable that you have Ye Olde English version... I definately recommend updating. :)
I just use the man pages while at work for quick reference. Some one needs to update them. (They don't list strchr and strrchr as conforming to ANSI.) Still, over all they're a pretty good reference.
(I've never seen a compiler that didn't have strchr, and had never looked for or used strrchr--actually, I very seldom need either function [never in the case of strrchr]--but I find it's better to not assume something is ANSI and find out later that it isn't.)
Quzah.