请求的代码评论 [英] Code comments solicited

查看:47
本文介绍了请求的代码评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Unixhead的简单版本。命令,

即显示文件前n行的实用程序。评论

欢迎...


#include< cstdlib>

#include< iostream>

#include< fstream>

#include< sstream>

#include< string>


使用namespace std;


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

{

if(argc< 2 || argc> 3){

// endl优于\ n在这里?

cerr<< 用法: << argv [0]<< " <文件> [线] QUOT; << endl;

返回EXIT_FAILURE;

}

ifstream f(argv [1]);

unsigned int count ;

如果(argc == 3){

//在回复我关于atoi的问题时建议()

(stringstream(argv) [2]))>>计数; //没有错误检查! :)

}

else {

count = 10;

}

if(!f){

// endl优于\ n在这里?

cerr<< 无法打开文件: << argv [1]<< endl;

返回EXIT_FAILURE;

}

string s;

while(!f.eof()& ;& count--){

getline(f,s);

//" \ n"首选?

cout<< s<< " \ n";

}

f.close();

返回EXIT_SUCCESS;

}


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

This is intended to be a simple version of the Unix "head" command,
i.e. a utility that displays the first n lines of a file. Comments
welcomed...

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main( int argc, char *argv[] )
{
if( argc < 2 || argc > 3 ) {
// endl preferable to "\n" here?
cerr << "Usage: " << argv[0] << " <file> [lines]" << endl;
return EXIT_FAILURE;
}
ifstream f( argv[1] );
unsigned int count;
if( argc == 3 ) {
// as suggested in responses to my question about atoi()
(stringstream(argv[2])) >> count; // no error checking! :)
}
else {
count=10;
}
if( !f ) {
// endl preferable to "\n" here?
cerr << "Could not open file: " << argv[1] << endl;
return EXIT_FAILURE;
}
string s;
while( !f.eof() && count-- ) {
getline( f, s );
// "\n" preferable?
cout << s << "\n";
}
f.close();
return EXIT_SUCCESS;
}

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:

[snip] string s;
while(!f.eof()&& count--){
getline(f,s);
[snip] string s;
while( !f.eof() && count-- ) {
getline( f, s );




eof不打算在这里使用它的方式。


用C ++读取文件的模式是:


while(read_as_long_as_there_is_no_error){

用读取的东西做什么

}


错误是什么?如果它是eof,那么一切都很好。


-------------------------


在你的情况下,没有必要在循环后检查eof,因为没有读到循环结束

是罚款和预期。


因此:


while(count--&& getline(f,s))

cout<< s<< ''\ n'';


//

//通常你会这样做

//

// if(!f.eof())

// cout<< 读取文件时出错了\ n;

//

另外:你不能检查argv [2]的转换是否为

int有效。但我相信你已经知道了。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Karl Heinz Buchegger< kb **** **@gascad.at>这样说:
Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while(count--&& getline(f,s))
cout<< s<< \\\
;
// if(!f.eof())
// cout<< 读取文件时出错了\ n;


<密度>等等,再解释一下为什么不需要......?< /密度>

另外:你不要''检查从argv [2]到
int的转换是否有效。但我相信你已经知道了。
while( count-- && getline( f, s ) )
cout << s << ''\n''; // if( !f.eof() )
// cout << "There was an error reading the file\n";
<denseness>Wait, explain again why this isn''t needed...?</denseness>
Also: you don''t check if the conversion from argv[2] into an
int has worked. But I''m sure you already know this.




是的,我在评论中表明了这一点:


- -

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Yes, I indicated this in a comment :)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.




" Christopher Benson-Manica" <在*** @ nospam.cyberspace.org>在消息中写道

news:bu ********** @ chessie.cirr.com ...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bu**********@chessie.cirr.com...
Karl Heinz Buchegger< kb *** ***@gascad.at>这样说:
Karl Heinz Buchegger <kb******@gascad.at> spoke thus:
while(count--&& getline(f,s))
cout<< s<< ''\ n'';
while( count-- && getline( f, s ) )
cout << s << ''\n'';


// if(!f.eof())
// cout<< 读取文件时出现错误\ n;
// if( !f.eof() )
// cout << "There was an error reading the file\n";



<密度>等等,再解释一下为什么不需要...?< / denseness>



<denseness>Wait, explain again why this isn''t needed...?</denseness>




因为在读取EOF后设置了流中的错误位。

因此你的程序再运行一次。


祝福,

Sharad



Because the error bit in stream is set after reading the EOF.
Thus your program runs one extra iteration.

Best wishes,
Sharad


这篇关于请求的代码评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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