我正在使用 Visual Studio 在 C 中创建一个项目,我需要它来读取文件并对文件中的文本进行分析。我必须将文件传递给命令行,但我不知道将文件放在哪里以便读取它。以下是帮助解释其工作原理的主要方法:
int main(int argc, char *argv[]){
FILE * cmdFile;
char * nxtCmd = (char *) malloc(sizeof(char) * STR_LEN);
//Ensure memory allocation for nxtCmd succeeds
if (nxtCmd == NULL){
puts("Memory failed to allocate for nxtCmd.");
exit(1);
}
// check if argc is 2
if(argc == MAX_ARGS){}
else{
puts("Innapropriate number of arguments passed to the program.");
exit(1);
}
// check that argv[1] can be opened
if ((cmdFile = fopen(argv[1], "r")) == NULL){
puts("Can't open the command file.");
exit(1);
}
else{
puts("Command file has opened successfully.");
}
// if both above are true:
// start reading from the cmdfile
while ((nxtCmd = nextLine(cmdFile)) != NULL){
nxtCmd = nextLine(cmdFile);
printf("next line: %s", nxtCmd);
}
}
如有任何帮助或意见,我们将不胜感激。
最佳答案
任何地方都可以,只需将它传递给命令行上的程序即可:
myprog.exe \path\to\file.txt
关于c - 在 Visual Studio 中放置要用作命令行参数的文件的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333769/