主要[文件IO] [英] Main [was File IO]

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

问题描述

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

int main(int argc,char * argv [] ){
FILE * in,* out;

if(argc == 1)
{
in = stdin;
out = stdout;
}

{
if(argc == 2)
{
in = stdin;
if((out = fopen(argv) [1],w))== NULL)
{
perror(输出文件创建);
退出(EXIT_FAILURE);
}
}

{
if((in = fopen(argv [1]," r"))== NULL)
{
perror("" ;输入文件打开);
退出(EXIT_FAILURE);
}
if((out = fopen(argv [2]," w"))== NULL)
{
perror("输出文件创建");
fclose(in);
退出(EXIT_FAILURE);
}
}
}

/ *这里的一些代码* /

返回EXIT_SUCCESS;
}



我正在看Giannis'我的代码在这里,我看到CI中的一件事最害怕

。我尽量避免看它。但是必须学习main(int

argc,char * argv [])。这些主要参数看起来像垃圾,当

会使用它们。 Argc必须表示main的参数,数组指针指向

char指针让我无能为力。我理解主要(无效)。什么是其他参数的

目的?


比尔


----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http:// www .newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000个新闻组--19个不同的服务器! = -----

解决方案

>当

使用时,这些主要参数对我来说就像垃圾一样他们。 Argc必须指一个main的参数,数组指针指向一个char指针让我无能为力。我理解主要(无效)。其他参数的目的是什么?




假设你打字时会发生什么


ls -l


Jan Engelhardt

-


Bill Cunningham写道:
< blockquote class =post_quotes>
我在这里看着Giannis的代码,我发现CI中的一件事最害怕
。我尽量避免看它。但是必须学习main(int
argc,char * argv [])。这些主要参数看起来像垃圾,当
将使用它们。 Argc必须指一个main的参数,数组指针指向一个char指针让我无能为力。我理解主要(无效)。其他参数的目的是什么?

Bill




Bill,argc参数告诉命令的数量传递给程序的行参数

(包括程序名)... char * argv []

是一个包含参数的char *数组。 。


所以strcmp(argv [0],myprog)在我们的例子中总是0,而最后的

元素是argv [argc-1] ] ...


即使你的myprog,如果你执行


#myprog

然后


argc = 1;和argv [] = {" myprog" };


如果你执行


#myprog我是一个狂热的


然后


argc = 6;和argv [] = {" myprog,i,am,a,c,freak等};


见K& R(他们解释得太多了)。


-

# include< stdio.h>

#define p(s)printf(#s" endian")

int main(void){int v = 1; *( char *)& v?p(小):p(大);返回0;}


Giannis Papadopoulos
http://dop.users.uth.gr/

塞萨利大学

计算机&通信工程部门




" Bill Cunningham" <无**** @ nspam.net>在消息中写道

news:40 ******** @ corp.newsgroups.com ...

Re:Main [was File IO ]


因为你的问题是关于一个完全不同的主题

来自这个主题的原始版本,你应该有

开始了新的主题。无论如何......见下文。

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


我'我在这里看Giannis的代码,我看到CI恐惧中的一件事



最多。我尽量避免看它。但是必须学习main(int
argc,char * argv [])。这些主要参数看起来像垃圾,当
将使用它们。 Argc必须表示main的参数,数组指针
到char指针让我一无所知。我理解主要(无效)。其他参数的目的是什么?




他们是用于访问程序的(通常是''命令行'' )

参数。


=========================== ======================= ============


ISO / IEC 9899:1999(E)

[....]


5.1.2.2.1程序启动

1程序启动时调用的函数名为main。

实现声明此函数没有原型。它应该使用返回类型int和没有

参数来定义



int main(void){/ * ... * /}


或两个参数(这里称为argc和argv,

虽然可以使用任何名称,因为它们是在声明它们的

函数的本地):


int main(int argc,char * argv []){/ * ... * /}


或等价物;(9)或其他一些实施定义的

方式。


2如果声明它们,主函数

的参数应遵守以下约束条件:


- argc的值应为非负值。


- argv [argc]应为空指针。

- 如果argc的值大于零,则数组

成员argv [0]到argv [argc-1]包含

包含指向字符串的指针,这些字符串由主机环境提供实现 -
tation定义的值为
程序启动。目的是向程序提供

在程序启动之前确定的信息来自托管环境中其他地方的
。如果主机envi-

ronment无法以大写和小写字母提供字母

,则执行将

确保字符串以小写形式接收。


- 如果argc的值大于零,则argv [0]指向的字符串

表示程序name;

argv [0] [0]如果程序名称

无法从主机环境获得,则为空字符。如果argc的值

大于1,则

argv [1]通过argv [argc-1]指向的字符串代表程序

参数。


- 参数argc和argv以及argv数组指向

的字符串应该可由程序修改,

并保留程序启动之间最后存储的值

和程序终止。


[....]


(9)因此,int可以替换为定义为int的typedef名称,

或argv的类型可以写为char ** argv,依此类推。


[....]


=================== =============================== ============

使用示例:


#include< stdio.h>


int main(int argc,char ** argv)

{

const char * const messages [] =

{

" [not available]",br />
" [none given]",

""

};


const char * const * pmsg [] = {messages,argv};

int i = 0;

printf(argc ==%d\\\
\ n,argc);


printf(" Program name:\ n%s \ nn \ n" ;,

argc> 0? * pmsg [** argv!= 0]

:消息[argc> 0]);


printf(" Parameters:\ n");


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

printf(" [%d] ==%s \ n",i,argv [i]);


printf("%s \ n",message [(argc> 1)+(argc> 0)]);

返回0;

}


-Mike


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

int main(int argc, char *argv[]) {
FILE *in, *out;

if(argc==1)
{
in = stdin;
out = stdout;
}
else
{
if(argc==2)
{
in = stdin;
if( (out = fopen(argv[1], "w"))==NULL )
{
perror("Output file creation");
exit(EXIT_FAILURE);
}
}
else
{
if( (in = fopen(argv[1], "r"))==NULL )
{
perror("Input file opening");
exit(EXIT_FAILURE);
}
if( (out = fopen(argv[2], "w"))==NULL )
{
perror("Output file creation");
fclose(in);
exit(EXIT_FAILURE);
}
}
}

/* some code here */

return EXIT_SUCCESS;
}


I''m looking at Giannis''s code here and I see the one thing in C I dread the
most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What''s the
purpose of the other parameters?

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

解决方案

>Those main parameters look like garbage to me, when

would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What''s the
purpose of the other parameters?



Suppose what happens when you type

ls -l

Jan Engelhardt
--


Bill Cunningham wrote:


I''m looking at Giannis''s code here and I see the one thing in C I dread the
most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What''s the
purpose of the other parameters?

Bill



Bill, the argc parameter tells the number of command line arguments that
where passed to the program (including program name)... The char *argv[]
is an array to char* that holds the arguments...

So strcmp(argv[0], "myprog") is always 0 in our case and the last
element is argv[argc-1]...

That is, being your "myprog", if you execute

# myprog

then

argc = 1; and argv[] = { "myprog" };

If you exec

# myprog i am a c freak

then

argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };

See K&R (they explain it more than enough).

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.



"Bill Cunningham" <no****@nspam.net> wrote in message
news:40********@corp.newsgroups.com...

Re: Main [was File IO]
Since your question is about an entirely different topic
from the original one of this thread, imo you should have
started a new thread. Whatever... See below.

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


I''m looking at Giannis''s code here and I see the one thing in C I dread


the most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to a char pointer leaves me clueless. I understand main(void). What''s the
purpose of the other parameters?



They''re for accessing a program''s (typically ''command line'')
parameters.

================================================== ============

ISO/IEC 9899:1999 (E)

[....]

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The
implementation declares no prototype for this function. It
shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;(9) or in some other implementation-defined
manner.

2 If they are declared, the parameters to the main function
shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.
-- If the value of argc is greater than zero, the array
members argv[0] through argv[argc-1] inclusive shall
contain pointers to strings, which are given implemen-
tation-defined values by the host environment prior to
program startup. The intent is to supply to the program
information determined prior to program startup from
elsewhere in the hosted environment. If the host envi-
ronment is not capable of supplying strings with letters
in both uppercase and lowercase, the implementation shall
ensure that the strings are received in lowercase.

-- If the value of argc is greater than zero, the string
pointed to by argv[0] represents the program name;
argv[0][0] shall be the null character if the program name
is not available from the host environment. If the value
of argc is greater than one, the strings pointed to by
argv[1] through argv[argc-1] represent the program
parameters.

-- The parameters argc and argv and the strings pointed to
by the argv array shall be modifiable by the program,
and retain their last-stored values between program startup
and program termination.

[....]

(9) Thus, int can be replaced by a typedef name defined as int,
or the type of argv can be written as char ** argv, and so on.

[....]

================================================== ============
Example use:

#include <stdio.h>

int main(int argc, char **argv)
{
const char * const messages[] =
{
"[not available]",
"[none given]",
""
};

const char * const *pmsg[] = {messages, argv};
int i = 0;
printf("argc == %d\n\n", argc);

printf("Program name:\n%s\n\n",
argc > 0 ? *pmsg[**argv != 0]
: messages[argc > 0]);

printf("Parameters:\n");

for(i = 1; i < argc; ++i)
printf("[%d] == %s\n", i, argv[i]);

printf("%s\n", messages[(argc > 1) + (argc > 0)]);
return 0;
}

-Mike


这篇关于主要[文件IO]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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