如何在C中正确使用系统

发布于 2025-02-01 10:51:10 字数 682 浏览 2 评论 0原文

我正在尝试与C一起打开Goog​​le的

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    system(" C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe ");
    return 0;
}

Chrome 将“启动”放在Google文件路径的前面时。

今天,在取出“启动”并离开文件路径之后,我遇到了错误:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: ` C:\Program Files (x86)\Google\Chrome\Application\chrome.exe '

我替换了Google的文件路径:

system("notepad");

它使记事本没有问题。

我使用GCC编译,然后使用./a.exe运行可执行文件,

我完全丢失了 - 有建议吗?

注意 这是第一次在这里问一个问题,所以如果我错过了任何有价值的信息,请让我知道

I'm trying to open Google's Chrome with C.

I'm using Cygwin bash as my terminal and have added it to my PATH - here's my code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    system(" C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe ");
    return 0;
}

Yesterday I had the problem of an error showing "sh: Start: Command not found" when putting "Start" in front of the google file path.

Today, after taking out the "Start" and just leaving the file path, I'm getting the errors:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: ` C:\Program Files (x86)\Google\Chrome\Application\chrome.exe '

I replaced the file path of google for:

system("notepad");

and it pulls up notepad no problem.

I compile using gcc then run the executable with ./a.exe

I'm completely lost - any advice?

NOTE
This is the first time asking a question here, so if I missed any valuable info please let me know

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2025-02-08 10:51:10
-- system(" C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe ");
++ system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\""); 

回想一下,默认情况下,空间是分隔线和分隔符,它们将命令行的不同部分划分。借助您的原始命令,您的命令行被解析为:

argv[0] : C:\Program
argv[1] : Files
argv[2] : (x86)\Google\Chrome\Application\chrome.exe

在其他单词中,它试图执行程序c:\ program,带有gragments files(x86) \ Google ....

通过添加围绕它的引号,您正在告诉shell(在您的情况下,可能cmd)您是 不是 试图执行c :\ program带有两个参数。

相反,引号澄清您要执行

“ c:\ program Files(x86)\ google \ chrome \ chrome \ application \ chrome.exe”作为可执行文件的一个重要路径,包括太空和名称中的帕伦斯(Parens)。

-- system(" C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe ");
++ system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\""); 

Recall that spaces, by default, are delimiters and separators that divide different parts of the command line. With your original command, your command line was parsed as:

argv[0] : C:\Program
argv[1] : Files
argv[2] : (x86)\Google\Chrome\Application\chrome.exe

In otherwords, it was trying to execute program C:\Program, with arguments Files and (x86)\Google.....

By adding quotes around it, you are telling the shell (likely CMD in your case) that you are not trying to execute C:\Program with two arguments.

Instead, the quotes clarify that you want to execute
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" as one big path to executable, including the spaces and parens within the name.

胡渣熟男 2025-02-08 10:51:10

摆脱后斜切,做一个

system("'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'")

system("'/cygdrive/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'")

Get rid of the backslashes and do a

system("'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'")

or a

system("'/cygdrive/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文