命令行参数,读取文件

标签 c file arguments command-line-arguments

如果我进入命令行 C: 我的程序我的文件.txt

如何在我的程序中使用 myfile。我是否必须扫描它或者是否有任意访问它的方式。

我的问题是如何在我的程序中使用 myfile.txt。

int
main(){
    /* So in this area how do I access the myfile.txt 
    to then be able to read from it./*

最佳答案

您可以使用 int main(int argc, char **argv) 作为主函数。

argc - 将是您程序的输入参数的计数。
argv - 将是指向所有输入参数的指针。

因此,如果您输入 C:\myprogram myfile.txt 来运行您的程序:

  • argc 将为 2
  • argv[0] 将是 myprogram
  • argv[1] 将是 myfile.txt

更多详情 can be found here

读取文件:
FILE *f = fopen(argv[1], "r");//"r"表示读取

要以其他模式打开文件,read this .

关于命令行参数,读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16869467/

相关文章:

c - C 格式的 Typedef 和 enum 以及优点

运行级别 3 上的 C 非阻塞键盘输入 block (Linux)

'file' 命令的 Perl 模块?

python - 使用函数更改参数值?

c - 生成错误 : multiple definition of

c - Shellcode c语言

c - C如何读取文件中新插入的内容?

ruby - 寻找在 Ruby 中轻松实现类文件/IO 类的方法

C 宏未按预期工作

Java:何时使用 String[] args 而不是 String args[]