命令行参数,读文件 [英] Command line arguments, reading a file

查看:118
本文介绍了命令行参数,读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我进入命令行
C:myprogram myfile.txt的

我怎么能在我的程序MYFILE使用。我必须scanf函数它还是有它访问的任意方式。

我的问题是如何利用myfile.txt的在我的计划。

  INT
主要(){
    / *所以在这方面我怎么访问的myfile.txt
    要那么能够从it./*阅读


解决方案

您可以使用 INT主(INT ARGC,字符** argv的)作为你的主要功能。

ARGC - 将是输入参数程序的计数结果
的argv - 将一个指向所有的输入参数。

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


  • ARGC 将2

  • 的argv [0] myprogram

  • 的argv [1] myfile.txt的

更多细节可以在这里找到

要读取的文件:结果
FILE * F =的fopen(的argv [1],R); //R读

有关开设在其它模式下的文件,阅读本

If i entered into the command line C: myprogram myfile.txt

How can I use myfile in my program. Do I have to scanf it in or is there an arbitrary way of accessing it.

My question is how can I use the myfile.txt in my program.

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

解决方案

You can use int main(int argc, char **argv) as your main function.

argc - will be the count of input arguments to your program.
argv - will be a pointer to all the input arguments.

So, if you entered C:\myprogram myfile.txt to run your program:

  • argc will be 2
  • argv[0] will be myprogram.
  • argv[1] will be myfile.txt.

More details can be found here

To read the file:
FILE *f = fopen(argv[1], "r"); // "r" for read

For opening the file in other modes, read this.

这篇关于命令行参数,读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆