   Fussing with command line arguments
  To start, the purpose of my program is to pass a filename to the program using argv, then to pass the program an output filename also using argv (such that when you type into the command prompt "MyProgram.exe C:\InFile.dat C:\OutFile.txt" it works).  I have passed a filename to my program using command-line parameters and am able to use the iostream class to read them with no problems. My problem is the fall-back method that is called when no output filename is specified. I want it to work such that if no output filename is given, the program will use the input filename, with a different file extension. The problem is, since argv is a character pointer array, and I am able to reach into the argv array and grabbing the input file name, how do I modify that file name (still in the format of a char*) such that the final three characters in the pointer are different? I would imagine that I'd somehow have to get the length of the input file name, and then maybe do (argv[2])[length-3] = 't' or something.Any help?[Edited by - LessBread on July 10, 2006 6:32:11 PM]
  If you mean the extension on windows you can use PathRenameExtension.  THere are a whole whack of file api's available for windows that are named Path****.And someone is probably going to ask you to rename your thread:)CheersChris   ;  Copy the name into a std::string, which you can then manipulate. find_last_of() can be used to fine the last . in the string.   ;  If your char*'s are \0 terminated, you can get the length using strlen(), after which you could do something like this:int len = strlen(inputFile) - 2; //length of file - 2 for extension (minus 2 because we want to start @ the first character of the extension)memcpy(inputFile + len, "txt", 3); //copy new extension into back of fileNote that this is not an ideal way to do this, just a soultion, and I have not tested the above code, its just off the top of my head, so I make no claims as to it actually working (though as far as I can tell it should).   ;  I'm assuming you're coding in C, not C++.I'd use strlen to determine the length of argv and strrchr to fine the '.'.  Then I'd create a new char array of the required size and use strcpy to copy argv over up to the '.' and then manually insert the file extension you want.  Or you could have the file extension in another char array and use strcat to concatenate them.Go here for info on the above named functions.Edit: Damn, you guys are fast.   ;Quote:Original post by yckxI'm assuming you're coding in C, not C++.He mentioned using iostream. That means C++.;Quote:Original post by FrunyHe mentioned using iostream. That means C++.I completely missed that :P;  Just felt I should mention that before you start messing around modifying the char arrays passed to your program via argv, DONT. Since these are allocated by the system prior to execution, it is a BAD IDEA to treat them as anything other than const. Declare main asint main(int argc,const char **argv);and copy them into your own arrays before you start altering them. Or even better, as has been suggested, copy them into std::strings.   ;  Copying them is trivial:#include <vector>#include <string>int main(int argc, char** argv_){    std::vector<std::string> argv(argv_, argv_+argc);    ...}