【问题标题】:Where to place files to be used as command line arguments in Visual Studio在 Visual Studio 中将文件用作命令行参数的位置
【发布时间】:2012-10-31 06:57:10
【问题描述】:

我正在使用 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);
}
}

任何帮助或 cmets 将不胜感激。

【问题讨论】:

    标签: c visual-studio file command-line


    【解决方案1】:

    任何地方都可以,只需在命令行中将其传递给程序即可:

    myprog.exe \path\to\file.txt
    

    【讨论】:

    • 如 mux 所说,假设文件是​​ C:\My Folder\File.txt,只需将其传递为 mypprog.exe "c:\My Folder\File.txt"
    猜你喜欢
    • 2015-07-03
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2018-12-04
    • 1970-01-01
    • 2010-09-22
    • 2019-06-08
    相关资源
    最近更新 更多