关于5303狄惟佳同学的myod程序设计的补充实现
原版代码实现的局限
int main(int argc,char *argv[])
{
if(strcmp(argv[1], "-tc")==0){
FILE *file=fopen(argv[3],"r");
tc(file);
}
if(strcmp(argv[2], "-tx1")==0){
FILE *file=fopen(argv[3],"r");
tx(file);
}
else if(strcmp(argv[2], "-to1")==0){
FILE *file=fopen(argv[3],"r");
to(file);
}
else if(strcmp(argv[2], "-td1")==0){
FILE *file=fopen(argv[3],"r");
td(file);
}
return 0;
}
- 通过阅读主函数,就可以知道本程序过于依赖argv的传参,而忽视argc的传参功能,从而导致本程序在用户使用上就成了,我只能按照
-tc -tx1 123.txt
或者-tc -to1 123.txt
这样的形式(即-tc必须在前,后面只能跟-tx1或者-to1,或者-td1)去输入,这就有点局限了,而linux上面真正是随便几个参数的输入,也随便顺序如何都可以按照用户的输入去调整输出形式,所以稍稍修改一下主函数之后是这样的
int main(int argc,char *argv[])
{
int i;
for(i=1;i<argc-1;i++){
if(strcmp(argv[i], "-tc")==0){
FILE *file=fopen(argv[argc-1],"r");
tc(file);
}
if(strcmp(argv[i], "-tx1")==0){
FILE *file=fopen(argv[argc-1],"r");
tx(file);
}
else if(strcmp(argv[i], "-to1")==0){
FILE *file=fopen(argv[argc-1],"r");
to(file);
}
else if(strcmp(argv[i], "-td1")==0){
FILE *file=fopen(argv[argc-1],"r");
td(file);
}
}
return 0;
}
- 第二个局限的地方就是后面用到的
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
函数里面的coord
值得设计了,如果只按照之前所说的,惟佳同学的设计,以及用户的“使用手则”的话,这样设初值是没有问题的,但如果主函数改变之后,就会出现行之间的覆盖的问题,这也就是光标设置错误导致的,下面是修改后的-tc函数的代码:
void tc(FILE *file,int n)
{
//Initialize the coordinates
COORD coord = {0, n};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
char ch[18];
int i=0,j=0;
while(fgets(ch,17,file)!=NULL){
printf("%07o",16*j);
j++;
for(i=0;i<16;i++)
{
if(ch[i]=='\n')
{ i++;
putchar(' ');
printf("\\n");
}
if(ch[i]=='\0')
break;
putchar(' ');
putchar(' ');
printf("%c", ch[i]);
putchar(' ');
}
int k;
for(k=0;k<count;k++)
printf("\n");
}
printf("%07o",16*(j-1)+i);
fclose(file);
}
- 此时主函数也需要改变(增加了全局变量count,用来记录命令行传参的个数减一,方便后续的换行,以达信息到不被覆盖):
int count=0;
int main(int argc,char *argv[])
{
int i;
count=argc-2;
for(i=1;i<argc-1;i++){
if(strcmp(argv[i], "-tc")==0){
FILE *file=fopen(argv[argc-1],"r");
tc(file,i-1);
}
if(strcmp(argv[i], "-tx1")==0){
FILE *file=fopen(argv[argc-1],"r");
tx(file,i-1);
}
else if(strcmp(argv[i], "-to1")==0){
FILE *file=fopen(argv[argc-1],"r");
to(file,i-1);
}
else if(strcmp(argv[i], "-td1")==0){
FILE *file=fopen(argv[argc-1],"r");
td(file,i-1);
}
}
return 0;
}
- 后面的几个函数也和-tc差不多(详见附录一)
- 下面是几个运行截图:
附录一
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
void tc(FILE *file,int n);
void tx(FILE *file,int n);
void to(FILE *file,int n);
void td(FILE *file,int n);
int count=0;
int main(int argc,char *argv[])
{
int i;
count=argc-2;
for(i=1;i<argc-1;i++){
if(strcmp(argv[i], "-tc")==0){
FILE *file=fopen(argv[argc-1],"r");
tc(file,i-1);
}
if(strcmp(argv[i], "-tx1")==0){
FILE *file=fopen(argv[argc-1],"r");
tx(file,i-1);
}
else if(strcmp(argv[i], "-to1")==0){
FILE *file=fopen(argv[argc-1],"r");
to(file,i-1);
}
else if(strcmp(argv[i], "-td1")==0){
FILE *file=fopen(argv[argc-1],"r");
td(file,i-1);
}
}
return 0;
}
void tc(FILE *file,int n)
{
//Initialize the coordinates
COORD coord = {0, n};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
char ch[18];
int i=0,j=0;
while(fgets(ch,17,file)!=NULL){
printf("%07o",16*j);
j++;
for(i=0;i<16;i++)
{
if(ch[i]=='\n')
{ i++;
putchar(' ');
printf("\\n");
}
if(ch[i]=='\0')
break;
putchar(' ');
putchar(' ');
printf("%c", ch[i]);
putchar(' ');
}
int k;
for(k=0;k<count;k++)
printf("\n");
}
printf("%07o",16*(j-1)+i);
fclose(file);
}
void tx(FILE *file,int n)
{
//Initialize the coordinates
COORD coord = {0, n};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
char ch[18];
int i;
while(fgets(ch,17,file)!=NULL){
printf(" ");
for(i=0;i<16;i++)
{
if(ch[i]=='\n')
{ i++;
printf("%3x ",'\n');
}
if(ch[i]=='\0')
break;
printf("%3x ",ch[i]);
}
int k;
for(k=0;k<count;k++)
printf("\n");
}
fclose(file);
}
void to(FILE *file,int n)
{
//Initialize the coordinates
COORD coord = {0, n};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
char ch[18];
int i;
while(fgets(ch,17,file)!=NULL){
printf(" ");
for(i=0;i<16;i++)
{
if(ch[i]=='\n')
{ i++;
printf("%03o ",'\n');}
if(ch[i]=='\0')
break;
printf("%03o ",ch[i]);
}
int k;
for(k=0;k<count;k++)
printf("\n");
}
fclose(file);
}
void td(FILE *file,int n)
{
//Initialize the coordinates
COORD coord = {0, n};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
char ch[18];
int i;
while(fgets(ch,17,file)!=NULL){
printf(" ");
for(i=0;i<16;i++)
{
if(ch[i]=='\n')
{ i++;
printf("%3d ",'\n');
}
if(ch[i]=='\0')
break;
printf("%3d ",ch[i]);
}
int k;
for(k=0;k<count;k++)
printf("\n");
}
fclose(file);
}