当两个不同参数中有空格时,C ++ system()不工作 [英] C++ system() not working when there are spaces in two different parameters

查看:468
本文介绍了当两个不同参数中有空格时,C ++ system()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个.exe需要一些参数使用system()。



如果.exe的路径和路径中有一个空间在参数传递的文件中,我得到以下错误:

 文件名,目录名或卷标语法不正确。 

以下是产生该错误的代码:

  #include< stdlib.h> 
#include< conio.h>

int main(){
system(\C:\\Users\\Adam\\Desktop\\pdftotext\-layout \C:\\Users\\Adam\\Desktop\\week 4.pdf\);
_getch();
}



如果pdftotext的路径不使用引号需要他们,因为有时目录将有空格),一切工作正常。另外,如果我把一个字符串中的system()中的内容,并输出它,我在一个实际的命令窗口复制它,它工作。



我可以链接一些命令使用这样的:

  cd C:\Users\Adam\Desktop; 
pdftotext -layoutweek 4.pdf

所以我已经是正确的目录,但我不知道如何在同一个system()函数中使用多个命令。



任何人都可以告诉我为什么我的命令不工作,

编辑:看起来我需要一组额外的引号,因为system()传递它的参数cmd / k,所以它需要在报价。我在这里找到了:



C ++:如何让我的程序用可选的参数打开一个.exe



解决方案

p> system() cmd / C命令运行命令。这里引用来自 cmd doc:

 如果/ C或/ K指定,那么
之后的命令行的剩余部分作为命令行处理,其中以下逻辑是
用于处理引号()字符:

1.如果满足以下所有条件,则保留命令行上的引号字符


- no / S switch
- 正好两个引号字符
- 两个引号字符之间没有特殊字符
其中special是以下之一:&<>()@ ^ |
- 在
之间有一个或多个空格字符两个引号字符
- 两个引号字符之间的字符串是可执行文件的名称


2.否则,旧行为是看第一个字符是否为
a引号字符,如果是,则剥离前导字符,
删除命令行上的最后一个引号字符,保留
在最后一个引号字符后面的任何文本。

看起来你碰到案例2, cmd 认为整个字符串 C:\Users\Adam\Desktop\pdftotext-layoutC:\Users\Adam\Desktop\week 4.pdf (即没有第一个和最后一个引号)是可执行文件的名称。



所以解决方案是将整个命令包装成额外的引号:

  //系统(\D:\\test\nospaces \ \); //给出与您获取
相同的错误system(\\D:\\test\nospaces \text with spaces \ \\); // ok,works

这很奇怪。我认为添加 / S 也是一个好主意,只是为了确保它总是解析字符串的情况2:

  system(cmd / S / C \\D:\\test\nospaces \text with spaces \\ ); // also works 


I'm trying to run a .exe that requires some parameters by using system().

If there's a space in the .exe's path AND in the path of a file passed in parameters, I get the following error:

The filename, directory name, or volume label syntax is incorrect.

Here is the code that generates that error:

#include <stdlib.h>
#include <conio.h>

int main (){
    system("\"C:\\Users\\Adam\\Desktop\\pdftotext\" -layout \"C:\\Users\\Adam\\Desktop\\week 4.pdf\"");
    _getch();
}

If the "pdftotext"'s path doesn't use quotation marks (I need them because sometimes the directory will have spaces), everything works fine. Also, if I put what's in "system()" in a string and output it and I copy it in an actual command window, it works.

I thought that maybe I could chain some commands using something like this:

cd C:\Users\Adam\Desktop;
pdftotext -layout "week 4.pdf"

So I would already be in the correct directory, but I don't know how to use multiple commands in the same system() function.

Can anyone tell me why my command doesn't work or if the second way I thought about would work?

Edit: Looks like I needed an extra set of quotation marks because system() passes its arguments to cmd /k, so it needs to be in quotations. I found it here:

C++: How to make a my program open a .exe with optional args

so I'll vote to close as duplicate since the questions are pretty close even though we weren't getting the same error message, thanks!

解决方案

system() runs command as cmd /C command. And here's citation from cmd doc:

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

It seems that you are hitting case 2, and cmd thinks that the whole string C:\Users\Adam\Desktop\pdftotext" -layout "C:\Users\Adam\Desktop\week 4.pdf (i.e. without the first and the last quote) is the name of executable.

So the solution would be to wrap the whole command in extra quotes:

//system("\"D:\\test\" nospaces \"text with spaces\"");//gives same error as you're getting
system("\"\"D:\\test\" nospaces \"text with spaces\"\""); //ok, works

And this is very weird. I think it's also a good idea to add /S just to make sure it will always parse the string by the case 2:

system("cmd /S /C \"\"D:\\test\" nospaces \"text with spaces\"\""); //also works

这篇关于当两个不同参数中有空格时,C ++ system()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆