檔案操作應用例項訓練之小小詞典

2021-09-03 08:12:16 字數 3292 閱讀 5724

由於本實驗主要是理解檔案操作,這裡先不把問題負責化。通常,詞典中的詞條包含單詞、音標、解釋、例句、短語等專案,電子詞典還可能會提供朗讀等資訊。這裡,只保留了其中最主要的兩項:單詞和解釋。

效果如下:

**如下:

#include#include#include#include#define default_filename "mydict.dat"

/*巨集定義*/

#define find_word_cmd 1

#define add_word_cmd 2

#define help_cmd 3

#define exit_cmd 4

#define unknown_cmd -1

/*函式的定義*/

void filereadline(file *file, char *buf);

void filewriteline(file *file, char *buf);

int dictfindword(file *file, char *word, char *exp);

void dictaddword(file *file, char *word, char *exp);

int decodecommand(char *cmdbuf, char *wordbuf, char *expbuf, int bufsize);

int getcommandcatagory(char cmdcatach);

void printhelpinfo();

/*主函式,程式入口*/

int main(int argc, char *argv)else

/*開啟字典檔案*/

dictfile = fopen(dictfilename, "a+");

/*如果開啟失敗,輸出錯誤資訊*/

if(dictfile == null)

printf("\n *** little console dictionary ***\n\n");

while(1)else

}/*如果輸入的命令過長,則列印錯誤資訊,並返回重新接收命令*/

if(res == -1)

/*接收的字串末尾新增"\0",以便程式正常處理*/

cmdbuf[i] = '\0';

/*對命令進行解碼,得到命令型別和引數*/

res = decodecommand(cmdbuf, wordbuf, expbuf, 80);

/*根據不同命令。進行不同的操作*/

switch(res)

/*進行單詞查詢,否則輸出錯誤資訊*/

res = dictfindword(dictfile, wordbuf, expbuf);

if(res == 1)else

break;

/*增加詞典詞條的命令處理*/

case add_word_cmd:

if(strlen(wordbuf) < 1 || strlen(expbuf) < 1)

/*新增單詞到詞典*/

dictaddword(dictfile, wordbuf, expbuf);

printf("add word \'%s\' into dictionary successfully.\n", wordbuf);

break;

/*輸出幫助資訊的命令處理*/

case help_cmd:

printhelpinfo();

break;

/*退出命令的處理*/

case exit_cmd:

/*輸出結束資訊*/

printf("goodbye\n");

/*關閉詞典檔案*/

fclose(dictfile);

return 0;

break;

default:

/*對非法命令的處理*/

printf("unknown command\n");

break;}}

/*雖然不會執行該語句,但是有時可以避免編譯器輸出警告資訊。*/

return 0;

}/*讀取檔案file中的一行文字,放到buf中。*/

void filereadline(file *file, char *buf)

*buf = '\0';

}/*向檔案file中寫入一行文字,寫入的內容為buf。*/

void filewriteline(file *file, char *buf)

/*在詞典檔案file中查詢單詞word,然後將解釋放到exp中。*/

int dictfindword(file *file, char *word, char *exp)

/*其他情況忽略解釋行。*/

filereadline(file, exp);

}return 0;

}/*新增單詞work,解釋exp到單詞詞典檔案file中。*/

void dictaddword(file *file, char *word, char *exp)

/*命令解碼,輸入命令為cmdbuf,輸出的第乙個引數放到wordbuf中,第二個引數

放到expbuf中,命令的型別由返回值返回,各個緩衝區的大小有bufsize確定。*/

int decodecommand(char *cmdbuf, char *wordbuf, char *expbuf, int bufsize)

nowch = *(cmdbuf++);

}wordbuf[i++] = '\0';

/*忽略到wordbuf之後的空白字元。*/

while(nowch != '\0' && isspace(nowch))

nowch = *(cmdbuf++);

/*將後面所有字元讀取到expbuf中。*/

j = 0;

while(nowch != '\0')

nowch = *(cmdbuf++);

}expbuf[j++] = '\0';

/*解釋命令型別字元的命令型別。*/

return getcommandcatagory(cmdcata);

}/*將命令型別字元翻譯成命令型別*/

int getcommandcatagory(char cmdcatach)

}/*輸出幫助資訊。*/

void printhelpinfo()

shell 例項之檔案操作

bin bash 建立檔案 if d testdir then mkdir testdir ficd testdir if f test.lua.bak.2017 then touch test.lua.bak.2017 fiif d subdir then mkdir subdir cd subd...

Python之檔案操作例項 批量修改檔案的字首名

匯入包,很重要的 import os 建立資料夾及檔案 os.mkdir test for i in range 1,6 os.mkdir test test str i fp open test test str i txt w encoding utf 8 while true print 功能...

C 例項應用(十六)資料夾基本操作

獲取資料夾資訊 獲取資料夾建立時間 directoryinfo類公開用於建立 移動和列舉資料夾和子資料夾的例項方法,其greationtime屬性用來獲取或設定當前directoryinfo物件的建立時間,該屬性的語法格式如下 public datatime creationtime屬性值 當前di...