argv []比较 [英] argv[] comparison

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

问题描述

进行argv []比较最安全的方法是什么?下面的代码

有效。


#include< iostream>

#include< string>


使用命名空间std;


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

{

字符串测试;


if(argc> 1)

test = argv [1];


if(test ==" NKDT")

cout<< \ n比较是真的。让程序做点什么。\ n" ;;

else

cout<< \ n比较是错误的。让程序做别的事情。\ n" ;;


返回0;

}

解决方案

* ka*****@hotmail.com
< blockquote class =post_quotes>
进行argv []比较最安全的方法是什么?下面的代码

有效。


#include< iostream>

#include< string>


使用命名空间std;


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

{

字符串测试;


if(argc> 1)

test = argv [1];


if(test ==" NKDT")

cout<< \ n比较是真的。让程序做点什么。\ n" ;;

else

cout<< \ n比较是错误的。让程序做别的事情。\ n" ;;


返回0;

}



执行以下操作:


#include< iostream>


#include< cstddef>

# include< string>

#include< vector>

#include< stdexcept>


typedef std :: vector< ; std :: stringStringVector;


bool throwX(char const s []){throw std :: runtime_error(s); }


void cppMain(StringVector const& arguments)

{

arguments.size()1

|| throwX(" usage:myprog ARG1");


if(arguments.at(1)==" NKDT")

{

//做点什么。

}

其他

{

//做别的。

}

}


int main(int n,char * a [])

{

试试

{

cppMain(StringVector(a,a + n));

返回EXIT_SUCCESS ;

}

catch(std :: exception const& x)

{

std :: cerr< ;< "!" << x.what()<< std :: endl;

返回EXIT_FAILURE;

}

}

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


5月21日下午2:39,Alf P. Steinbach < a ... @ start.nowrote:


* kafe ... @ hotmail.com:


< blockquote class =post_quotes>
进行argv []比较最安全的方法是什么?下面的代码

有效。


#include< iostream>

#include< string>


using namespace std;


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

{

string测试;


if(argc> 1)

test = argv [1];


if(test ==" NKDT")

cout<< \ n比较是真的。让程序做点什么。\ n" ;;

else

cout<< \ n比较是错误的。让程序做别的事情。\ n" ;;


返回0;

}



以下:



< snip>


OP的解决方案出了什么问题,看起来很多更多

简洁?


* da *********** @ fastmail.fm


5月21日下午2:39, Alf P. Steinbach < a ... @ start.nowrote:


> * kafe ... @ hotmail.com:


>>>
进行argv []比较最安全的方法是什么?下面的代码
有效。
#include< iostream>
#include< string>
使用命名空间std;
int main(int argc,char * argv [])
{
字符串测试;
if(argc> 1)
test = argv [1];
if(test ==" NKDT")
cout<< \ n比较是真的。让程序做某事。\ n" ;;
其他
cout<< \ n比较是错误的。让程序执行其他操作。\ n" ;;
返回0;
}


执行以下操作:



< snip>


OP的解决方案出了什么问题,看起来更加简洁?b $ b简洁明了吗?



可能直接错误:始终表明程序成功

执行。也可能是直接错误:不处理异常。

但是,有人可能会设计一个程序,这样就不会有任何异常

传播到main并且如果它执行一个希望终止以便于调试,而不是报告和失败指示。所以它取决于设计

和方法和工具链以及某种程度上的个人偏好。

但我不认为这里缺乏异常处理是故意的。 />

Ungood:主要代码可以直接访问argc和argv,当目标

是以最安全的方式处理它们时(提供访问是不安全的)。另外

ungood一般:using namespace std:;。另外ungood:不使用

花括号用于嵌套语句(几乎任何样式指南都会

告诉你使用它们,总是,为了支持维护)。 />

简洁的外观:所有OP的代码都是程序特定的,

而我列出的代码主要是样板文件,一种

针对这种特殊小型学生计划的微框架或

专业人员的签出计划或一次性个人工具计划。

为了保持一致,可以写出例如(做/同/作为OP的代码)


#include< string>

#include< iostream>

int main(int n,char * a [])

{

using namespace std;

n 1&& std :: string(a [1])==" NKDT"

? cout<< \ n比较是真的。 Something.\\\
"

:cout<< \ n比较是错误的。其他东西。\ n" ;;

}


摆脱局部变量''test''以及''返回0''哪个

暗示(这是默认的)''main'',只是为了使它更加便宜

简洁我也删除了那个愚蠢的''其他''否则会耗尽

非常昂贵的行,而关键字''if''则需要阅读。我只是不明白为什么这么多程序员认为简洁是一个

的目标。他们最终没有理解他们自己的代码。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e中最烦人的事情是什么邮件?


What is the safest way to make an argv[] comparison? The code below
works.

#include <iostream>
#include <string>

using namespace std;

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

if(argc>1)
test = argv[1];

if(test=="NKDT")
cout << "\nComparison is true. Have program do something.\n";
else
cout << "\nComparison is false. Have program do something else.\n";

return 0;
}

解决方案

* ka*****@hotmail.com:

What is the safest way to make an argv[] comparison? The code below
works.

#include <iostream>
#include <string>

using namespace std;

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

if(argc>1)
test = argv[1];

if(test=="NKDT")
cout << "\nComparison is true. Have program do something.\n";
else
cout << "\nComparison is false. Have program do something else.\n";

return 0;
}

Do the following:

#include <iostream>

#include <cstddef>
#include <string>
#include <vector>
#include <stdexcept>

typedef std::vector<std::stringStringVector;

bool throwX( char const s[] ) { throw std::runtime_error( s ); }

void cppMain( StringVector const& arguments )
{
arguments.size() 1
|| throwX( "usage: myprog ARG1" );

if( arguments.at(1) == "NKDT" )
{
// Do something.
}
else
{
// Do something else.
}
}

int main( int n, char* a[] )
{
try
{
cppMain( StringVector( a, a+n ) );
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


On May 21, 2:39 pm, "Alf P. Steinbach" <a...@start.nowrote:

* kafe...@hotmail.com:


What is the safest way to make an argv[] comparison? The code below
works.

#include <iostream>
#include <string>

using namespace std;

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

if(argc>1)
test = argv[1];

if(test=="NKDT")
cout << "\nComparison is true. Have program do something.\n";
else
cout << "\nComparison is false. Have program do something else.\n";

return 0;
}


Do the following:

<snip>

What was wrong with the OP''s solution, which appeared to be much more
concise?


* da***********@fastmail.fm:

On May 21, 2:39 pm, "Alf P. Steinbach" <a...@start.nowrote:

>* kafe...@hotmail.com:

>>>
What is the safest way to make an argv[] comparison? The code below
works.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string test;
if(argc>1)
test = argv[1];
if(test=="NKDT")
cout << "\nComparison is true. Have program do something.\n";
else
cout << "\nComparison is false. Have program do something else.\n";
return 0;
}

Do the following:


<snip>

What was wrong with the OP''s solution, which appeared to be much more
concise?

Probably directly wrong: always indicating success for the program
execution. Also probably directly wrong: doesn''t handle exceptions.
However, one might design a program so that no exception should ever
propagate up to main and if it does one wants termination for ease of
debugging, not a report and failure indication. So it depends on design
and methodology and toolchain and to some extent personal preference.
But I don''t think the lack of exception handling here was intentional.

Ungood: the main code has direct access to argc and argv, when the aim
is to handle them in "the safest way" (giving access is unsafe). Also
ungood in general: "using namespace std:;". Also ungood: not using
curly braces for nested statements (just about any style guideline will
tell you to use them, always, in order to support maintenance).

Appearance of conciseness: all of the OP''s code is program-specific,
whereas the code I listed is mostly boilerplate, a kind of
micro-framework for this particular kind of small student program or
professional''s check-it-out program or single-use personal tool program.
For consisness one could write e.g. (doing the /same/ as the OP''s code)

#include <string>
#include <iostream>
int main( int n, char* a[] )
{
using namespace std;
n 1 && std::string( a[1] ) == "NKDT"
? cout << "\nComparison is true. Something.\n"
: cout << "\nComparison is false. Something else.\n";
}

getting rid of the local variable ''test'' and also the ''return 0'' which
is implied (it''s the default) in ''main'', and just to make it extra
concise I also removed that darned ''else'' which otherwise would use up a
very expensive line, and the keyword ''if'' which is so much to read. I
simply don''t understand why so many programmers think conciseness is a
goal. They just end up not understanding their own code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


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

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