Thread: How to truncate URL string to filename?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    Question How to truncate URL string to filename?

    Yup, I'm a newbie.

    I've looked up some of the functions, but I can't seem to put them together. Could someone lay it all out in simple terms?

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    What do you mean by truncate a URL to a file name?
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    if truncate is the same as concatenation. look up strcat(.....) found in string.h
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Originally posted by Dr. Bebop
    What do you mean by truncate a URL to a file name?
    Sorry. I'm trying to write a simple wget frontend (common newbie project, I suspect). I have a full url stored in a string, and I need to reduce it to just the filename:

    http://www.host.com/pic01.jpg

    to

    pic01.jpg

    As I said, I know it has something to do with reading the string backwards and finding the '/'; but I can't quite sort it out.

    Thanks.

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    If it's a C string you can use the functions in <cstring>.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
            char *URL = "http://www.host.com/pic01.jpg";
            char *file_name;
    
            file_name = strrchr( URL, '/' ) + 1;
    
            cout<< file_name <<endl;
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    Should consider the case when there are more than one"/" in the URL, I did similar thing in a project on linux. I used GnuString,which is very easy.

    As to the standard C++, just search for the last "/" then you got the file name

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Thanks Dr. Bebop. Works great. I was thinking I'd need two functions, one to find the position of / and the other to do the truncation; so I was completely barking up the wrong tree.

    crystalike: You're right, I should have chosen a better example. However, strrchr() does work with longer URL's, so I guess it must read the string backwards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM