【发布时间】:2015-09-30 17:33:19
【问题描述】:
我正在尝试编写一个程序,该程序使用一些参数在同一文件夹中运行其他可执行文件,这个 exe 是来自 poppler-utils 的 pdftotext.exe,它会生成一个文本文件。
我准备一个字符串作为system()的参数传递,结果字符串是:
cd/D N:\folder0\folder1\folder2\foldern && pdftotext.exe data.pdf -layout -nopgbrk
先进入文件所在目录,然后运行可执行文件。
当我运行它时,我总是得到
sh: cd/D: No such file or directory
但如果我直接从命令提示符运行该命令,则该命令有效。
我认为这并不重要,但这是我目前所写的:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
// Function to get the base filename
char *GetFileName(char *path);
int main (void)
{
// Get path of the acutal file
char currentPath[MAX_PATH];
int pathBytes = GetModuleFileName(NULL, currentPath, MAX_PATH);
if(pathBytes == 0)
{
printf("Couldn't determine current path\n");
return 1;
}
// Get the current file name
char* currentFileName = GetFileName(currentPath);
// prepare string to executable + arguments
char* pdftotextArg = " && pdftotext.exe data.pdf -layout -nopgbrk";
// Erase the current filename from the path
currentPath[strlen(currentPath) - strlen(currentFileName) - 1] = '\0';
// Prepare the windows command
char winCommand[500] = "cd/D ";
strcat(winCommand, currentPath);
strcat(winCommand, pdftotextArg);
// Run the command
system(winCommand);
// Do stuff with the generated file text
return 0;
}
【问题讨论】:
-
可能想要创建进程而不是系统
-
@EdHeal,谢谢,我试过
createProcess(),但没有运气,我不太明白那个函数的参数是什么意思,特别是最后两个,欢迎任何例子。跨度> -
您使用的是哪个编译器/环境?赛格温?
标签: c windows cmd cygwin system