15.8 十六進制傾印碼函數
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char **argv) { //輸入流 FILE *source; //左側行號區域 int line = 0; //讀入的個數 size_t readCount; //循環計數 int idx; //一個字節的16進制字符串 char hex[4]; //16進制顯示區域,32個16進制加上3個空白 char hexArea[36] = {'\0'}; //字符顯示區域 char alphaArea[17]; //存放讀取數據 char buffer[17]; if(argc > 1){ //argv第二個是參數 source = fopen(*(argv + 1), "rb"); }else{ //若是沒有文件名,標準輸入當作輸入源 source = stdin; } //source流檢查 if(source == NULL){ printf("[%s] %d\n", *(argv + 1) , argc); perror("\n"); exit(EXIT_FAILURE); } //循環直到文件結束 while(!feof(source)){ //讀取16個字節,若是沒有讀取剩下的字節 readCount = fread(buffer, sizeof(char), 16, source); for(idx = 0; idx < readCount; idx++){ //轉換成大寫16進制 sprintf(hex, "%02X", buffer[idx]); //四個字節一個空格 if(idx > 0 && (idx % 4) == 3){ strcat(hex, " "); strcat(hexArea, hex); }else{ strcat(hexArea, hex); } if(isprint(buffer[idx])){ alphaArea[idx] = buffer[idx]; }else{ alphaArea[idx] = '.'; } } alphaArea[readCount] = '\0'; printf("%06x %-36s*%-16s*\n", line * 16, hexArea, alphaArea); line++; hexArea[0] = '\0'; alphaArea[0] = '\0'; } return 0; }
/home/mao/test文件內容:命令行
運行:blog
15.9 fgrep實現ip
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_BUF 510 int main(int argc, char ** argv) { FILE *file; char buff[MAX_BUF]; char search[MAX_BUF]; char currentFile[MAX_BUF]; int lineNumber; //目標字符串 strcpy(search, *(argv + 1)); if(argc > 2){ //文件打開 argv += 2; while(*(argv) != NULL){ strcpy(currentFile, *argv); file = fopen(currentFile, "r"); if(file == NULL){ perror(currentFile); exit(EXIT_FAILURE); } lineNumber = 1; while(fgets(buff, MAX_BUF, file) != NULL){ //讀取文件之後,進行比對,若是找到打印信息,若是沒有,繼續讀 if(strstr(buff, search) != NULL){ printf("%-10s:%-5d %s\n", currentFile, lineNumber, buff); } lineNumber++; } //下一個文件 argv++; } }else{ //標準輸入打開 while(fgets(buff, MAX_BUF, stdin) != NULL){ if(strstr(buff, search) != NULL){ printf("%s %s\n", search, buff); }else{ printf("no found %s in %s\n", search, buff); } } } return 0; }
目錄下三個文件1.txt 2.txt 3.txt,運行:資源
15.10 校驗checkSum字符串
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_BUF 100 int main(int argc, char ** argv) { FILE *file; FILE *cksFile; char cksFileName[MAX_BUF]; char currentFile[MAX_BUF]; int ch; int isToFile = 0; unsigned short int checkNum; if(argc > 1){ //文件輸入,檢查是否 -f if(strcmp(*(argv + 1), "-f") == 0){ isToFile = 1; //檢查-f是否合法 if(argc == 2){ printf("-f illegal when reading standard input"); exit(EXIT_FAILURE); } //指向-f後的文件名 argv += 2; }else{ argv += 1; } //開始校驗文件 while(*argv != NULL){ //保存當前文件名 strcpy(currentFile, *argv); if(isToFile){ //生成並打開cks文件流 strcpy(cksFileName, currentFile); strcat(cksFileName, ".cks"); cksFile = fopen(cksFileName, "w"); if(cksFile == NULL){ perror(""); exit(EXIT_FAILURE); } } //打開須要讀入的文件 file = fopen(currentFile, "r"); if(file == NULL){ perror(""); exit(EXIT_FAILURE); } //開始計算checkSum checkNum = 0; while((ch = fgetc(file)) != EOF){ checkNum += ch; } //輸出checkSum if(isToFile){ fprintf(cksFile, "%5s checkSum: %hu", currentFile, checkNum); fclose(cksFile); }else{ printf("%5s checkSum: %hu", currentFile, checkNum); } //關閉文件流,並準備打開下一個文件 fclose(file); argv++; } }else{ //處理標準輸入 checkNum = 0; while((ch = getchar()) != EOF){ checkNum += ch; } printf("%u\n", checkNum); } return 0; }
運行:get
15.11 商品存貨記錄input
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_DES_BUF 40 #define MAX_BUF 20 #define SUCCESS 1 #define FAILED 0 //單件商品信息結構體 typedef struct { //商品描述 char description[MAX_DES_BUF]; //商品數量 unsigned int quantity; //商品價格 float price; //商品總價值 float totalPrice; //已售利潤 float profit; //是否已刪除標示 short int isDeleted; //行號 unsigned int lineNum; } GoodsMsg; //添加記錄成功返回 SUCCESS 失敗FAILED int newRecord(char *order, FILE *file); //購買 int buyRecord(char *order, FILE *file); //出售 int sellRecord(char *order, FILE *file); //刪除 int delRecord(char *order, FILE *file); //打印 int printPartRecord(char *order, FILE *file); //打印全部 void printRecord(FILE *file); //計算總價值 float totalPrice(FILE *file); int main(int argc, char **argv) { FILE *file; char FileName[MAX_DES_BUF]; char order[MAX_DES_BUF]; //沒有指定文件名,生成記錄文件 if(argc == 1){ strcpy(FileName, "D:\\GoodsRecord.txt"); }else{ strcpy(FileName, *(argv + 1)); } //打開文件流,若是文件不存在,則從新生成文件 file = fopen(FileName, "rb+"); if(file == NULL){ //新建文件 file = fopen(FileName, "wb+"); if(file == NULL){ perror(""); exit(EXIT_FAILURE); }else{ printf("Create File %s", FileName); } }else{ printf("open file %s", FileName); } printf("$:"); //讀取命令 while(fgets(order, 40, stdin) != NULL){ //匹配命令 if(strncmp(order, "end", 3) == 0){ //結束 printf("bye bye\n"); break; }else if(strncmp(order, "new", 3) == 0){ //添加記錄 if(newRecord(order, file) == SUCCESS){ printf("\nnew success: %s\n", order); } }else if(strncmp(order, "buy", 3) == 0) { if(buyRecord(order, file) == SUCCESS){ printf("buy: %s\n", order); } }else if(strncmp(order, "sell", 4) == 0) { if(sellRecord(order, file) == SUCCESS){ printf("sell: %s\n", order); } }else if(strncmp(order, "delete", 6) == 0) { if(delRecord(order, file) == SUCCESS){ printf("del: %s\n", order); } }else if(strncmp(order, "print all", 9) == 0) { printRecord(file); }else if(strncmp(order, "print", 5) == 0) { printPartRecord(order, file); }else if(strncmp(order, "total", 5) == 0) { printf("total: %f\n", totalPrice(file)); }else{ printf("unknow order\n"); } printf("$:"); } fclose(file); return 0; } //添加記錄成功返回 SUCCESS 失敗FAILED int newRecord(char *order, FILE *file) { //須要寫入文件商品信息結構體 GoodsMsg *good = (GoodsMsg *)malloc(sizeof(GoodsMsg)); //文件讀取的臨時結構體 GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); //行號 unsigned int lineNum = 1; //商品價格 float price = 0; //商品數量 unsigned int quantity; //商品描述 char descript[MAX_BUF] = {'\0'}; //重置流的位置從頭開始讀取 rewind(file); //將命令行格式化輸入參數中 if(sscanf(order, "new %s , %u , %f\n", descript, &quantity, &price) == 3){ //初始化數據; strcpy(good -> description, descript); good -> quantity = quantity; good -> price = price; good -> totalPrice = price * quantity; good -> profit = 0; good -> isDeleted = 0; //尋找插入位置,寫在已刪除商品位置,仍是文件末尾添加 while(fread(temp, sizeof(GoodsMsg), 1, file)){ //查找已刪除產品 if(temp -> isDeleted == 1){ //定位到該條信息前,準備覆蓋信息 fseek(file, -sizeof(GoodsMsg), SEEK_CUR); break; }else{ //wb+模式中,讀以後若是須要寫,則需定位到當前位置 fseek(file, 0L, SEEK_CUR); } lineNum++; } //添加行號 good -> lineNum = lineNum; //寫入商品信息 fwrite(good, sizeof(GoodsMsg), 1, file); //寫入緩衝並釋放資源 fflush(file); free(temp); free(good); return SUCCESS; } return FAILED; } //購買 int buyRecord(char *order, FILE *file) { //讀取的商品結構體 GoodsMsg *good = (GoodsMsg *)malloc(sizeof(GoodsMsg)); unsigned int partNumber; unsigned int quantity; float price; rewind(file); //命令符合格式 if(sscanf(order, "buy %u , %u , %f\n", &partNumber, &quantity, &price) == 3) { //查看數據更新位置 while (fread(good, sizeof(GoodsMsg), 1, file)) { //查找產品位置 if (good->lineNum == partNumber) { //定位到該條信息前 fseek(file, -sizeof(GoodsMsg), SEEK_CUR); //從新計算均價和商品總價值 good -> totalPrice = (good -> totalPrice) + quantity * price; good -> quantity = (good -> quantity) + quantity; good -> price = (good -> totalPrice) / good -> quantity; //更新商品信息 fwrite(good, sizeof(GoodsMsg), 1, file); fflush(file); return SUCCESS; } } //其餘狀況都返回FAILED } return FAILED; } //出售 int sellRecord(char *order, FILE *file) { GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); unsigned int partNumber; unsigned int quantity; float sellPrice; rewind(file); //命令符合格式 if(sscanf(order, "sell %u , %u , %f\n", &partNumber, &quantity, &sellPrice) == 3) { //查看數據更新位置 while (fread(temp, sizeof(GoodsMsg), 1, file)) { //查找產品 if (temp->lineNum == partNumber) { //定位到該條信息前,準備覆蓋信息 fseek(file, -sizeof(GoodsMsg), SEEK_CUR); temp -> totalPrice = (temp -> totalPrice) - quantity * (temp -> price); temp -> quantity = (temp -> quantity) - quantity; temp -> profit = temp -> profit + (sellPrice - temp -> price) * quantity; fwrite(temp, sizeof(GoodsMsg), 1, file); fflush(file); return SUCCESS; } } //其餘狀況都返回FAILED } return FAILED; } //刪除,只需將isDelete置爲1 int delRecord(char *order, FILE *file) { GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); unsigned int partNumber; rewind(file); //命令符合格式 if(sscanf(order, "delete %u\n", &partNumber) == 1) { //查看數據更新位置 while (fread(temp, sizeof(GoodsMsg), 1, file)) { //查找產品 if (temp->lineNum == partNumber) { //定位到該條信息前,準備覆蓋信息 fseek(file, -sizeof(GoodsMsg), SEEK_CUR); temp -> isDeleted = 1; fwrite(temp, sizeof(GoodsMsg), 1, file); fflush(file); return SUCCESS; } } //其餘狀況都返回FAILED } return FAILED; } //打印 int printPartRecord(char *order, FILE *file) { GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); unsigned int partNumber; rewind(file); //命令符合格式 if(sscanf(order, "print %u\n", &partNumber) == 1) { //查看數據更新位置 while (fread(temp, sizeof(GoodsMsg), 1, file)) { //在未刪除產品中查找 if (temp->lineNum == partNumber && temp -> isDeleted == 0) { //打印信息 printf("\nid:%4u %u %.2f %.2f %20s\n", temp -> lineNum, temp -> quantity, temp -> price, temp -> totalPrice, temp -> description); return SUCCESS; } } //其餘狀況都返回FAILED } return FAILED; } //打印全部 void printRecord(FILE *file) { GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); rewind(file); printf("\nid quan price totalPric descprition\n"); //命令符合格式 while (fread(temp, sizeof(GoodsMsg), 1, file)) { //打印全部未刪除商品 if(temp -> isDeleted == 0){ printf("id:%-4u%-4u %.2f %.2f\t%-20s\n", temp -> lineNum, temp -> quantity, temp -> price, temp -> totalPrice, temp -> description); } } printf("--------------------------------------------\n"); } //計算總價值 float totalPrice(FILE *file) { float totalPrice = 0; GoodsMsg *temp = (GoodsMsg *)malloc(sizeof(GoodsMsg)); rewind(file); //命令符合格式 while (fread(temp, sizeof(GoodsMsg), 1, file)) { //打印信息 totalPrice += temp -> totalPrice; } return totalPrice; }
運行:string
注意:若是wb+打開文件須要讀取,那麼在讀取以前須要fflush或文件定位函數,fseek,fsetpos, rewind。若是rb+打開文件,須要寫入,必須調用文件定位函數,fseek,fsetpos, rewind。產品