Thread: Remove the extension of a file passed as a parameter (argv[1])

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    3

    Remove the extension of a file passed as a parameter (argv[1])

    I'm implementing a program that needs to use a function from another module that has 3 parameters:

    Header from the function I need to use:

    Code:
    int create_torrent_from_metainfo_file (char const * const metainfo_file_name, struct torrent_t * const torrent,
            char const * const downloaded_file_name);
    Main:
    Code:
    int main(int argc, char **argv) {
    struct torrent_t torrent;
        if(argc == 2) {
            if(create_torrent_from_metainfo_file(argv[1], &torrent, downloaded_file_name) {
    ....
            }
    }


    Given that when I'll run the program I'll pass as an argument a metainfo file (which is a .ttorrent file), I need the 3rd parameter in the function to be the name of this metainfo file without the .ttorrent extension (the name of argv[1] without the extension), but I'm very lost and I don't know how to do it.

    Any idea?

    Thank you in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,657
    You can use strrchr to find the last '.' in a string.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by Dante404 View Post
    I'm implementing a program that needs to use a function from another module that has 3 parameters:

    Header from the function I need to use:

    Code:
    int create_torrent_from_metainfo_file (char const * const metainfo_file_name, struct torrent_t * const torrent,
            char const * const downloaded_file_name);
    Main:
    Code:
    int main(int argc, char **argv) {
    struct torrent_t torrent;
        if(argc == 2) {
            if(create_torrent_from_metainfo_file(argv[1], &torrent, downloaded_file_name) {
    ....
            }
    }


    Given that when I'll run the program I'll pass as an argument a metainfo file (which is a .ttorrent file), I need the 3rd parameter in the function to be the name of this metainfo file without the .ttorrent extension (the name of argv[1] without the extension), but I'm very lost and I don't know how to do it.

    Any idea?

    Thank you in advance.

    You want to work with a copy of the command line argument. It's
    not a good idea to modify the argv[] strings in place.

    So call malloc() with strlen(argv[1]) + 1 (for the nul), then strcpy.
    That gives you a copy of the string.

    Now work backwards and overwrite the work string with nuls,
    until you come to a dot character. Be sure to also test for string
    start so you don't crash the program if passed an argument without
    an extension.

    You might need to be slightly more complicated, for example
    an extension can usually occur only in the last portion of the file
    path, as delimited by slash characters.

    Once you've got the modified path, pass it as usual to the torrent
    function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a file with a name passed as a parameter
    By sigur47 in forum C Programming
    Replies: 5
    Last Post: 05-05-2012, 07:42 PM
  2. How passed as an input parameter?
    By Siaw Ys in forum C Programming
    Replies: 3
    Last Post: 12-03-2011, 06:06 AM
  3. Object passed to method as parameter
    By elodman in forum C# Programming
    Replies: 11
    Last Post: 09-22-2011, 07:59 PM
  4. remove(const char* ) only with extension?
    By mosu' in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2006, 01:35 PM
  5. Comparing A Struct To A Passed Parameter
    By Anonymous in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2002, 11:38 PM

Tags for this Thread