Hello, I am trying to send directory names through the command line to program I am writing. This works perfectly until I add a space to the directory. I have tried adding Quotes around the directories and everything. The problem is then with the \ at the end of the directory it makes the quote literal and puts it in my string.

Is there any way to read in a directory from command line with a space in it? Maybe I shouldn't add the slash at the end of the directory or something? Any ideas would be great.

Thank you very much,
Cameron

Recommended Answers

All 4 Replies

I'm not sure whether I fully understood your question, but
consider the following program

#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
	if(2 == argc)
	{
		cout << "received: (" << argv[1] << ")" << endl;
	}

	return 0;
}

Let's say that the program executable is named arg1.exe, and you invoke it as follows:

arg1.exe "this string contains spaces"

you should see the following output:

received: (this string contains spaces)

That is what I have so far, I am actually trying to send the arguments from another program in a createprocess statement. Either way. What I am trying to send is C:\FOLDER\DEEPERFOLDER\BOTTOM FOLDER\ What happens from what I can tell though is the \ makes the " literal and sends it too making one huge command.

int main(int argc, char *argv[])
{
  int i;
  char* file_name_in = 0;
  char* file_name_out = 0;
  char* directory_name_in = 0;
  char* directory_name_out = 0;
  char separator_sign = ' ';
  char* parse_string = "xyz";
  char printstring[256];
  char* temp_dir = getenv("TMP");
  strcat(temp_dir, "\\");

  std::string tempnames[17];

	for (i = 1; i < argc; i++)
  {
	if (strcmp(argv[i],"-h") == 0)
	{
	  usage();
	}
	else if (strcmp(argv[i],"-parse") == 0)
    {
      i++;
      parse_string = argv[i];
    }
	else if (strcmp(argv[i],"-sep") == 0)
	{
	  i++;
	  if (strcmp(argv[i],"komma") == 0)
	  {
		separator_sign = ',';
	  }
	  else if (strcmp(argv[i],"tab") == 0)
	  {
		separator_sign = '\t';
	  }
	  else if (strcmp(argv[i],"dot") == 0 || strcmp(argv[i],"period") == 0)
	  {
		separator_sign = '.';
	  }
	  else if (strcmp(argv[i],"colon") == 0)
	  {
		separator_sign = ':';
	  }
	  else if (strcmp(argv[i],"scolon") == 0 || strcmp(argv[i],"semicolon") == 0)
	  {
		separator_sign = ';';
	  }
	  else if (strcmp(argv[i],"hyphen") == 0 || strcmp(argv[i],"minus") == 0)
	  {
		separator_sign = '-';
	  }
	  else if (strcmp(argv[i],"space") == 0)
	  {
		separator_sign = ' ';
	  }
	  else
	  {
		fprintf(stderr, "ERROR: unknown seperator '%s'\n",argv[i]);
		usage();
	  }
	}
	else if (strcmp(argv[i],"-i") == 0)
	{
	  i++;
	  directory_name_in = argv[i];
	}
	else if (strcmp(argv[i],"-o") == 0)
	{
	  i++;
	  directory_name_out = argv[i];
	}
	else if (i == argc - 2 && file_name_in == 0 && file_name_out == 0)
	{
	  directory_name_in = argv[i];
	}
	else if (i == argc - 1 && file_name_in == 0 && file_name_out == 0)
	{
	  directory_name_in = argv[i];
	}
	else if (i == argc - 1 && file_name_in && file_name_out == 0)
	{
	  directory_name_out = argv[i];
	}
  }

Could you post the code that wraps CreateProcess?

I actually fixed the problem by changing it so that the program didn't require \ at the end of the directory.

Thank you for the help though!

Cameron

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.