範例
void
doEverything(
int
argc,
char
* argv[])
{
/*
*
* 針對實體函數長度而提出的註解。
*
*
*
這個方法會有 13 個陳述式,用來進行陳述式計數。
*
* */
if
(argc>1){
cout <<
"Reading all files"
;
for
(
int
i=1; i<argc; i++){
cout <<
"reading file "
<< argv[i] << endl;
char
temp[1000];
fstream file(argv[i], ios::in);
cout <<
"file contents are "
<< endl;
while
(!file.eof()){
file.getline(temp, 1000);
cout << temp;
}
file.close();
}
}
else
{
cout <<
"No files specified"
;
}
}
解決方案
將函數縮小,使它更明確。標頭和行內註解應該足以提供說明。
void
readOneFile(
char
* fileName);
/*
*
* readEachFile(int argc, char* argv[]) 函數的標頭
* 讀取給定的檔案清單。
*
* */
void
readEachFile(
int
argc,
char
* argv[])
{
if
(argc>1){
//the first parameter is the executable
cout <<
"Reading all files"
;
for
(
int
i=1; i<argc; i++){
cout <<
"reading file "
<< argv[i] << endl;
readOneFile(
argv[i]
);
}
}
else
{
cout <<
"No files specified"
;
}
}
void
readOneFile(
char
* fileName){
char
temp[1000];
fstream file(fileName, ios::in);
cout <<
"file contents are "
<< endl;
while
(!file.eof()){
file.getline(temp, 1000);
cout << temp;
}
file.close();
}