gpt4 book ai didi

c - 运行一个程序,我将文件名传递给 Main

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:53 24 4
gpt4 key购买 nike

好吧,我假设这个答案就在那里,但是有很多人在发布代码。我找不到它实际实现的地方。我正在使用代码块,并将文件名传递给 main。但是当我点击运行时,没有传入文件名。在Codeblocks中如何传入文件名?我使用 cmd promt 还是什么?我想传入 input.txt 但我想不通。现在为了防止它崩溃,我只是将它默认为 input.txt。

nfile = fopen(argv[1], "r+");

// Check for file open
if (nfile == NULL) {
nfile = fopen("input.txt", "r+");

printf("FILE NAME: %s argv\n", argv);

if (nfile == NULL) {
printf("Failed to open file. Must have file name (input.txt)\n");
return -1;
}
}

最佳答案

int main(int argc, char*argv[])

^ 这应该是函数头,要使用它,您必须使用命令提示符传递参数。

例如:如果您使用 gcc 来编译程序。然后使用gcc yourprogram.c

之后使用./a.out input.txt运行代码

关于c - 运行一个程序,我将文件名传递给 Main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27112956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com