命令行参数,读文件 [英] Command line arguments, reading a file
问题描述
如果我进入命令行
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 2argv[0]
will bemyprogram
.argv[1]
will bemyfile.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屋!