我试图编写一个程序,运行其他可执行文件与一些参数在同一个文件夹中,这个exe是pdftotext.exe
来自poppler-utils,它会生成一个文本文件。从另一个exe运行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;
}
可能想要createprocess而不是系统 –
@EdHeal,谢谢,我试过'createProcess()'但没有运气,我不太明白什么意思是该函数的参数,特别是最后两个,任何示例都是受欢迎的。 – wallek876
网上有很多例子,比如https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx –