使用filnames作为命令行参数的帮助 [英] help needed with filnames as command line arguments

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

问题描述

我想在命令行中传递两个文件名。


例如:


my_executable filename_1 filename_2


到目前为止我还没有使用命令行参数进行任何C编程。


我熟悉fopen,ascii模式,二进制模式。我正在做什么

现在是在源代码中指定文件名。

所以我为每个源代码文件指定了不同的名称

每次重新编译。


文件中的数据是签名的,我用ascii模式读取文件,

不是二进制文件。


另外,我很高兴有一些关于

命令行参数编程或简单示例的参考资料。


我有K& R,Harbison和Steele'的CA参考手册和

我知道在哪里可以找到comp.lang.c的常见问题


谢谢


Dvid Bernier

I''d like to pass on the command line two filenames.

As for example:

my_executable filename_1 filename_2

I haven''t done any C programming with command line arguments so far.

I''m familiar with fopen, ascii mode, binary mode. What I''m doing
now is specifying the filenames in the source code.
So I give different names to each source code file
and recompile each time.

The data in the files is signed ints and I read from files in ascii mode,
not binary.

Also, I''d be glad to have some references about
command line argument programming or simple examples.

I have K&R, Harbison and Steele''s ``C A Reference Manual" and
I know where to find the FAQ for comp.lang.c

thanks

Dvid Bernier

推荐答案

" David Bernier" <哒****** @ sympatico.ca>在消息中写道

新闻:_k ********************* @ news20.bellglobal.com ...
"David Bernier" <da******@sympatico.ca> wrote in message
news:_k*********************@news20.bellglobal.com ...
我想在命令行中传递两个文件名。

此外,我很高兴有一些关于
命令行参数编程或简单示例的参考资料。
I''d like to pass on the command line two filenames.

Also, I''d be glad to have some references about
command line argument programming or simple examples.




main()的一个规范是:

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

>
有了它,你可以接收命令行args,argv [0]到argv [argc - 1]。例如,尝试



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

{

int i;

for(i = 0; i< argc; i ++)

puts(argv [i]);

返回0 ;

}


希望它有所帮助。



One specification for main() is:
int main(int argc, char *argv[])

With that, you can receive command line args, argv[0] to argv[argc - 1]. Try
this for instance:
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
puts(argv[i]);
return 0;
}

Hope it helps.


David Bernier< da **** **@sympatico.ca>在消息新闻中写道:< _k ********************* @ news20.bellglobal.co m> ...
David Bernier <da******@sympatico.ca> wrote in message news:<_k*********************@news20.bellglobal.co m>...
我' '我想在命令行中传递两个文件名。

例如:

my_executable filename_1 filename_2

我还没有做过到目前为止使用命令行参数进行C编程。
I''d like to pass on the command line two filenames.

As for example:

my_executable filename_1 filename_2

I haven''t done any C programming with command line arguments so far.




#include< stdio.h>

#include< stdlib.h>


/ *这将打印出所有命令行参数,然后退出* /

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

{

int n;


for(n = 0; n< argc; n ++)

{

printf(" argument%d:%s \ n",n,argv [n]);

}


返回0;

}


以上代码未经测试&未编译。 YMMV :-)



#include <stdio.h>
#include <stdlib.h>

/* this will print out all command line arguments, then exit */
int main(int argc, char[] *argv)
{
int n;

for (n = 0; n < argc; n ++)
{
printf("argument %d: %s\n", n, argv[n]);
}

return 0;
}

The above code is untested & uncompiled. YMMV :-)


在您的源代码中,使用此原型定义main。


main(int args,char * argv [])

{

int i;

printf("可执行文件:%s",argv [0]);

printf("参数数量为:%d",args);

for(i = 1; i< args; i ++)

{

printf(" Argument%d is:%s \ n",i,argv [i]);

}

}


希望这会有所帮助。


问候,

Rama Krishna。


David Bernier< da ****** @ sympatico.ca>在消息新闻中写道:< _k ********************* @ news20.bellglobal.co m> ...
In your source code, define main with this prototype.

main(int args, char *argv[])
{
int i;
printf("executable file : %s", argv[0]);
printf("Number of arguments is : %d", args);
for(i=1;i<args;i++)
{
printf("Argument %d is : %s\n", i, argv[i]);
}
}

Hope this helps.

Regards,
Rama Krishna.

David Bernier <da******@sympatico.ca> wrote in message news:<_k*********************@news20.bellglobal.co m>...
我' '我想在命令行中传递两个文件名。

例如:

my_executable filename_1 filename_2

我还没有做过到目前为止用命令行参数进行C编程。

我熟悉fopen,ascii模式,二进制模式。我正在做什么
现在是在源代码中指定文件名。
所以我给每个源代码文件指定不同的名称
并重新编译。
文件中的数据是签名的,我用ascii模式读取文件,
不是二进制文件。

另外,我很乐意提供一些关于
的参考资料。命令行参数编程或简单示例。

我有K& R,Harbison和Steele的CA参考手册。
我知道在哪里可以找到comp.lang.c的常见问题



Dvid Bernier
I''d like to pass on the command line two filenames.

As for example:

my_executable filename_1 filename_2

I haven''t done any C programming with command line arguments so far.

I''m familiar with fopen, ascii mode, binary mode. What I''m doing
now is specifying the filenames in the source code.
So I give different names to each source code file
and recompile each time.

The data in the files is signed ints and I read from files in ascii mode,
not binary.

Also, I''d be glad to have some references about
command line argument programming or simple examples.

I have K&R, Harbison and Steele''s ``C A Reference Manual" and
I know where to find the FAQ for comp.lang.c

thanks

Dvid Bernier



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

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