I searched the web for these, but couldn't find anything.
1) How do you find a file's extention (type)?
2) How do you link strings together in one variable or string?
(eg. newstring = oldstring1 + oldsting2 [is it even possible?])
I searched the web for these, but couldn't find anything.
1) How do you find a file's extention (type)?
2) How do you link strings together in one variable or string?
(eg. newstring = oldstring1 + oldsting2 [is it even possible?])
In C++, you can do #2 with the std::string datatype.
For #1, just start at the end of a filename string and step back until you get a fullstop or the beginning of the string.
Code:#include <iostream> #include <string> using namespace std; int main() { string s1 = ("hello"); string s2 = ("world"); string s3 = s1 + " " + s2 + "!"; cout << s3 << endl; cin.get(); return 0; }
I found strcat() to solve my problem.
> I found strcat() to solve my problem.
Then you're not really using C++ to it's best advantage - check ILoveVectors post.
> 1) How do you find a file's extention (type)?
Find the last '.' in the filename?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>1) How do you find a file's extention (type)?
something like this
Code:#include <iostream> #include <string> using namespace std; int main(){ string filename = "blah.mp3"; string::iterator i = filename.end(); while ( *i != '.' ){ i--; } cout << "The extension is " << string( i, filename.end() ); return 0; }
Last edited by dra; 08-19-2005 at 02:24 AM.
dra, your code derefences the invalid iterator returned by end(). You would want to decrement it first. Also, you should use rfind() instead of your own hand-written loop to avoid small mistakes like that.