Thread: Trying to use argv to access multiple files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    strncpy(new_filename, old_filename, argv[j]-ext+1);
    What value does the expression "argv[j]-ext+1" have?

    And to expand a bit on my warning earlier about strncpy(): From what I'm guessing you're trying to copy, from old_filename, everything up to the extension. Thus there will never be a null character in the source and so a string will never be created (we'll assume that your strrchr() call always returns a non-null pointer).

    If this is indeed the case, then I'd highly recommend memcpy() instead of strncpy(). strncpy() really was designed to be use in a specific case in unix, where an array of char was usually null terminated (thus a string) except in the case where the length of the string (not counting the null character) was exactly the length of the array; then the entire array was filled and no null character stored.

    Its name, unfortunately, tags it as a string handling function which it is only sporadically. By using memcpy() you're not tricking yourself into thinking you're dealing with a string. If you do decide to use strncpy(), there are a couple of things to keep in mind:

    The third argument (the size argument) must, absolutely must be no larger than the size of the array you're passing in as the first argument. Otherwise you might overrun the array, which is no good business. Generally the third argument to strncpy() is used to give the size of the first array, much the way snprintf() is used (though snprintf() is smarter than strncpy()).

    Once you've called strncpy(), and you want a string (which is almost always, in those rare cases that you should use strncpy()), then you must null-terminate it yourself. The general call to strncpy() should look something like:
    Code:
    strncpy(target, source, n);
    target[n] = 0;
    In this case, if target is 100 bytes, then n should be 99. By doing this, you're guaranteeing a string has been created.

    Of course, the way you're using strncpy() is not the normal method (from what I can tell), but as I mentioned, a method for which memcpy() is probably better; you're essentially using strncpy() as a poor man's memcpy(). You are limiting the source length, not the target length (which you hope is long enough to hold what you're copying).

    I'm not certain this whole rambling post is very useful to you, so the two things I urge you to keep in mind are: strncpy() is a function usually best left alone, but if you must use it, remember to null terminate your strings manually.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    My main question is: if I use "*.txt" as argv[1], will the program (when it functions correctly) go through all .txt files?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by DonFord81 View Post
    My main question is: if I use "*.txt" as argv[1], will the program (when it functions correctly) go through all .txt files?
    it is upto you what you program will do with the string "*.txt"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    Quote Originally Posted by vart View Post
    it is upto you what you program will do with the string "*.txt"
    Does that mean I have to write a routine to have the program figure out what "*.txt" means?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by DonFord81 View Post
    Does that mean I have to write a routine to have the program figure out what "*.txt" means?
    yes, it does
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

Tags for this Thread