Thread: Handling spaces in command line arguments

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    Handling spaces in command line arguments

    Hi Folks,

    A question which I hope isn't too silly or naive but one that is pulling my hair out.

    I'm passing arguments to main, no big deal. The first argument that I'm passing is a directory where input txt files need to be processed by my program. Again nothing complicated or anything.

    THe problem is windows and specfically spaces that it allows in directory names.

    For example, I call my program with the below command lines.

    Code:
    C:\AutoChar\\autochar_classes.exe C:\Documents and Settings\adminstrator\My documents\turn57
    Notice the spaces in the Documents and Settings directory and My Documents.

    Now I only want to have 1 extra element in argv but because of the spaces, it adds 4 of them. As below:-

    Code:
    argc = 5
    argv[0] = C:\AutoChar\autochar_classes.exe
    argv[1] = C:\Documents
    argv[2] = and
    argv[3] = Settings\adminstrator\My
    argv[4] = documents\turn57
    So is there any syntax within DOS that I can use so that the spaces are ignored by argv and that it puts the entire command line argument at argv[1] (spaces and alll).

    If not in DOS, then is there something that I can do within c++.

    My problem is that I will have 2 command line arguments. One with be the input directory for input files and the 2nd command line argument will be the directory for output files. Both may contain white space in their names and I won't know for sure when one begins and one ends.

    Any suggestions ?

    Cheers
    Starkhorn
    Last edited by starkhorn; 11-11-2004 at 05:39 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quotes perhaps.. maybe i'm not understanding clearly. I think what you are wanting is something like:
    my_program.exe "C:\Documents and Settings\User\My Documents\info.txt"

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    11
    This should work:
    Code:
    C:\AutoChar\\autochar_classes.exe C:\Docume~1\adminstrator\mydocu~1\turn57

  4. #4
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    oh dear oh dear.....how silly do I feel now.

    Yes quotes worked and the ~1 worked as well. Talk about a silly starfish moment.

    Thanks for the swift replies and sorry for asking such a naive question.

    Cheers
    Starkhorn

  5. #5
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    Hi Folks,

    Yes me again with another silly question although this time I hope its not as silly as before.

    The below is fairly simple, I've written a small program (at the end of the thread), whose aim is to build a dos excution command which calls an external .exe program.

    There is one argument being passed into Main. For my testing, the argument in argv[1] is:-

    Code:
    "C:\Documents and Settings\adminstrator\Mes documents\MiddleEarth\mepbm\Game63\turn0\test.pdf"
    It's a pdf file and my program calls an external exe called pdftext.exe which converts the pdf file to a text file. I found the pdftext.exe on the web and it's in the same directory as argv[0].

    So after extracting the various directory paths using simple subtr's and find functions, I call a function which returns a string that is the execution string. This is then called with the system call.

    The execution string is as below:-

    Code:
    "C:\Documents and Settings\adminstrator\Mes documents\exec_str\test\Debug\pdftext.exe" -layout "C:\Documents and Settings\adminstrator\Mes documents\MiddleEarth\mepbm\Game63\turn0\test.pdf"
    however, when I call this with:-

    Code:
    system(exe_str.c_str());
    It fails, stating that "C:\Documents " is an unknown filename. I don't understand this as the first part of the execution string is double quotes to get around spaces in the directory name.

    Even more strange, is if I cut and paste the execution string into the dos prompt, it works fine. Yet in my c++ program, it does not work.

    Any ideas why ? I guess that I could change the directory name to replace the long-names with ~1 stuff but was hoping that I was doing something silly again.

    Anyway, see below the full program.

    Many thanks in advance.

    Cheers
    Starkhorn

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    #define PDF_EXE "pdftext.exe"
    
    string set_exec_str(string pdf_filename, string pdf_dir, string exe_dir);
    
    int main(int argc, char *argv[])
    {	
    	
    	
    	string pdf_dir, exe_dir, pdf_filename, exe_str;
    	
    
    //assigns command line arguments to local variables. argv[1] 
    //is the pdf name with full directory path
    	
    	exe_dir = argv[0];
    	pdf_dir = argv[1];
    
    //gets the pdf name from pdf_dir.
    
    	pdf_filename = pdf_dir.substr(pdf_dir.find_last_of("\\")+1, pdf_dir.size());
    	
    //gets just the exe_dir by dropping the name of the c++ program
    //just gets the directory where main.exe is stored
    
    	exe_dir = exe_dir.substr(0, exe_dir.find_last_of("\\"));
    
    //gets just the pdf_dir without the name of the pdf file itself
    
    	pdf_dir = pdf_dir.substr(0, pdf_dir.find_last_of("\\"));
    
    //calls function which builds the execution str
    
    	exe_str = set_exec_str(pdf_filename, pdf_dir, exe_dir);
    
    	system(exe_str.c_str());
    
    	return 1;
    }
    
    string set_exec_str(string pdf_filename, string pdf_dir, string exe_dir)
    {
    	string str;
    
    	
    	str += "\"";
    	str += exe_dir;
    	str += "\\";
    	str += PDF_EXE;
    	str += "\"";
    	str += " -layout";
    
    	str += " ";
    	str += "\"";
    	str += pdf_dir;
    	str += "\\";
    	str += pdf_filename;
    	str += "\"";
    	
    	return str;
    }

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Here's an example that executes "hello.exe" in my folder C:\Documents and Settings. I put a couple of extra " surrounding the path name. If something like this works for you, you can build your own, as you did in your program.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    int main()
    {
      string st("\"\"C:\\Documents and Settings\\hello.exe\"\"");
      cout << "st: <" << st << ">" << endl;
      cout << "st.c_str(): <" << st.c_str() << ">" << endl;
      system(st.c_str());
      return 0;
    }
    Regards,

    Dave
    Last edited by Dave Evans; 11-16-2004 at 02:13 PM.

  7. #7
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    Thanks Dave. Adding an additional \" to the front my my string and to the end seemed to do the trick.

    I don't quite understand why though but if it works then I'm not touching it.

    Thanks for your help.

    Cheers
    Starkhorn

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by starkhorn
    Thanks Dave. Adding an additional \" to the front my my string and to the end seemed to do the trick.

    I don't quite understand why though but if it works then I'm not touching it.

    Thanks for your help.

    Cheers
    Starkhorn
    My thinking was: A small program that shows you that there is a way to do something should be a springboard for your learning.

    Someday you may want to do something similar but not exactly the same. My point is that you can always make up a small program to see what it takes to feed a string (or whatever) to other functions. Then you can try lots of things without getting lost in your big program.

    Oh, well, if it works for you, it works. (As someone once said, "If it ain't broke, you can't fix it." Or something like that...

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  3. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  4. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  5. Strategies for Handling Command Line Arguments
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2008, 08:18 AM