0
我有一個程序根據它們的簽名識別文件。很棒。但我是C新手,現在正試圖讓一個名爲CLion的IDE工作。然而,我無法弄清楚如何將命令參數添加到cmake中 - 例如,當我運行main並希望傳遞gifFile或pdfFile來識別我可以這樣做時。這是我的代碼迄今爲cmake。Cmake傳遞參數
cmake_minimum_required(VERSION 3.8)
project(Assignment6)
set(CMAKE_C_STANDARD 99)
set(SOURCE_FILES file_recognizer.c)
add_executable(Assignment6 ${SOURCE_FILES})
在命令行
這會是這樣的
gcc file_recognizer.c -o Assignment6
,然後你說
./Assignment6 gifFile.gif
如何獲得CMake的末接受的說法,「的gifFile。 GIF「?
如果需要
int main(int argc, char const *argv[]) {
FILE *yourFile;
unsigned char *fileBytes, *fileType;
long fileLength;
fileLength = 10;
if(argc != 2 || (yourFile = fopen(argv[1], "rb")) == NULL) {
printf("Invalid Input\n");
exit(-1);
}
fileBytes = readFile(yourFile, fileLength, fileBytes);
determineFileType(fileBytes, fileType);
return 0;
}
感謝。這適用於傳遞參數,但由於某種原因,它現在位於argv [0]。必須與我的其他機器上編譯與gcc的方式有關。欣賞小費雖然! – Avallauch