我正在使用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);
}
}
非常感谢任何帮助或评论。
答案 0 :(得分:1)
随时随地将其传递给命令行上的程序:
myprog.exe \path\to\file.txt