通過cmd[3]=0、strcpy(file, cmd + 4)實現命令的解析,比較詭異。
必須要知道命令頭只有三個位元組的秘密
若將來命令格式發生變化,麻煩大了
只有在while迴圈中改動,除了要理解命令解析外,還要理解其他很多語義
另外,其他命令解析,也需要進行strcpy,重複就是罪惡!
需要把命令解析的功能提取處理
另乙個問題:要輸入「-ld c:\test」後,再輸入「-ld」,可以發現「c:\test」依然存在
單步除錯,觀察cmd在記憶體中的變化
在scanf之前,對cmd進行清0操作
// 2.17.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#include #include const unsigned int max_length_of_cmd = 300;
const unsigned int max_length_of_cmd_head = 10;
const char* default_file_name = "temp.txt";
/*strdefaultfilefullpath要確保足夠的儲存空間,建議為max_path
*/bool getdefaultfilefullpath(char *strdefaultfilefullpath, const char *strdefaultfilename, const char *strargv0)
if(i < 0)
else
return true;
}void displaydefaultfile(const char *strdefaultfilefullpath)
fclose(fp);
} else }
/*確保strcommandhead和strparameter有足夠的空間,建議分別設為max_length_of_cmd_head、max_length_of_cmd
*/bool interpretcommand(const char* strcommand, char *strcommandhead, char *strparameter)
void entercommandloop()
fflush(stdin);
char cmd_head[max_length_of_cmd_head];
char cmd_parameter[max_length_of_cmd];
memset(cmd_head, 0, max_length_of_cmd_head);
memset(cmd_parameter, 0, max_length_of_cmd);
if(!interpretcommand(cmd, cmd_head, cmd_parameter))
printf("your command is : %s\n", cmd);
if(strcmp(cmd_head, "-ld") == 0)
if(strcmp(cmd_head, "-sa") == 0)
if(strcmp(cmd_head, "-se") == 0)
if(strcmp(cmd_head, "-ad") == 0)
if(strcmp(cmd_head, "-qt") == 0)
printf("false command\n"); }}
int _tmain(int argc, _tchar* argv)
我們做的改進有消除了每個if else 裡面的一句重複** ,置0後解決了-ld 問題 函式封裝之後接解決了 如果命令的格式發生變化,只是需要改變函式裡面的數字 3 就可以了
iPhone讀電話本
abaddressbookref addressbook abaddressbookcreate cfarrayref allpeople abaddressbookcopyarrayofallpeople addressbook cfindex npeople abaddressbookgetpe...
電話本再實現
標頭檔案模組 define crt secure no warnings 1 實現乙個通訊錄 通訊錄可以用來儲存1000個人的資訊,每個人的資訊包括 姓名 性別 年齡 住址 ifndef contact h define contact h include include define name m...
電話本 新增錯誤命令處理
如果輸入錯誤命令,返回提示資訊 2.18.cpp 定義控制台應用程式的入口點。include stdafx.h include include const unsigned int max length of cmd 300 const unsigned int max length of cmd ...