1:使用用基于文件指針的文件操作函數,實現把文本文件a.txt中的內容復制到b.txt中,b.txt中原來的內容被覆蓋。 答案: #include int main() { FILE *fpa = fopen("a.txt", "rb"); FILE *fpb = fopen("b.txt", "wb"); char ch; while((ch = fgetc(fpa)) != EOF) { fputc(ch, fpb); } fclose(fpa); fclose(fpb); return 0; } 2:用基于文件描述符的文件操作函數,實現自己的簡單的cp命令:mycp, 命令格式如:./mycp a.txt b.txt,把a.txt復制為b.txt。 要能實現單個文件的復制,以及從指定路徑復制文件到指定路徑中。 答案: #include #include #include #include #include #include int main(int argc, char *argv[]) { char buf[512] = {0}; int fo1 = open(argv[1], O_RDONLY); int fo2 = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0755); if(fo2 == -1) { printf("error! file exist!\n"); exit(0); } int fr = 0; /*開始復制*/ while( (fr = read(fo1, buf, sizeof(buf))) > 0 ) { write(fo2, buf, fr); } close(fo1); close(fo2); return 0; } 3:從命令行傳入某個.c或.txt文件的文件名,實現以下功能。 1):對該文件的每個字符做如下變換:如果是大寫字母就變?yōu)樾懀绻切懽帜妇妥優(yōu)榇髮,并寫回原文件,其它字符不變?br /> 2):輸出該文件的大小,文件類型,鏈接數,最后修改時間。 3):測試當前用戶對該文件的權限(讀、寫、執(zhí)行)。 要求:模塊化編程,用函數實現不同的功能模塊,再通過函數指針調用相關函數。 主函數只做:打開文件,調用函數,關閉文件。 另外,對文件讀寫之前,要加鎖,讀寫完畢后,再解鎖。 答案: /*源代碼*/ #include #include #include #include #include #include #include #include /*大小寫互相轉換*/ void zhuanhuan(int fd) { char c; struct flock lock = {F_WRLCK, SEEK_SET, 0, 0, getpid()}; if(-1 == fcntl(fd, F_SETLK, &lock)) { perror("lock failed!\n"); exit(-1); } while((read(fd, &c, sizeof(char)) > 0)) { if(c >= 'A'&& c <= 'Z') c = c + 'a' - 'A'; else if(c >= 'a' && c <= 'z') c = c - 32; else continue; lseek(fd, -1, SEEK_CUR); write(fd, &c, sizeof(char)); } lock.l_type = F_UNLCK; if(-1 == fcntl(fd, F_SETLK, &lock)) { perror("unlock failed!\n"); exit(-1); } } /*檢測權限*/ void quanxian(char *filename) { if(!access(filename, F_OK)) { if(!access(filename, R_OK)) printf("r"); else printf("-"); if(!access(filename, W_OK)) printf("w"); else printf("-"); if(!access(filename, X_OK)) printf("x"); else printf("-"); } else printf("file not exist!\n"); } /*獲取文件信息*/ void xinxi(int fd) { struct stat a; if(-1 == fstat(fd, &a)) { perror("fstat error"); exit(-1); } if(S_ISREG(a.st_mode)) printf("-"); else if(S_ISDIR(a.st_mode)) printf("d"); else if(S_ISLNK(a.st_mode)) printf("l"); else if(S_ISCHR(a.st_mode)) printf("c"); else if(S_ISBLK(a.st_mode)) printf("b"); else if(S_ISFIFO(a.st_mode)) printf("p"); else if(S_ISSOCK(a.st_mode)) printf("s"); printf(" %d", a.st_nlink); printf(" %d", a.st_size); struct tm *pTm; pTm = gmtime(&a.st_mtime); printf(" %04d-%02d-%02d",(1900 + pTm->tm_year), (1 + pTm->tm_mon), (pTm->tm_mday)); printf(" %02d:%02d:%02d\n",(8 + pTm->tm_hour), (pTm->tm_min), (pTm->tm_sec)); } /*該函數調用一系列函數實現各個功能*/ void diaoyong(int fd, char *filename) { /*定義函數指針*/ void (*pfun1)(int); void (*pfun2)(char *); void (*pfun3)(int); pfun1 = zhuanhuan; pfun2 = quanxian; pfun3 = xinxi; pfun1(fd); pfun2(filename); pfun3(fd); } int main(int a, char *b[]) { int fd = open(b[1], O_RDWR); if(-1 == fd) { perror("open failed!\n"); exit(-1); } diaoyong(fd, b[1]); close(fd); return 0; } 4:先用fork函數創(chuàng)建一個子進程,然后父進程從鍵盤輸入一個字符串,在子進程中輸出這個字符串。編寫程序過程中,注意避免造成孤兒進程和僵尸進程。要求用之前學的知識去實現。 答案: #include #include #include #include #include #include int main() { char buf[50] = {0}; int pid = fork(); if(pid > 0) { printf("父進程:Enter a string:"); scanf("%s", buf); int fd = open("a.txt",O_WRONLY|O_CREAT); write(fd, buf, sizeof(buf));//父進程把字符串寫入文件 close(fd); waitpid(pid, NULL, 0); } else { sleep(5); int fd = open("a.txt",O_RDONLY); read(fd, buf, sizeof(buf));//子進程可以從該文件中讀出該字符串. close(fd); printf("子進程:The string is:%s\n", buf); } return 0; } 5:寫一個顯示時間的程序,終端下動態(tài)顯示時間,并且時間在走。 答案: #include #include int main() { time_t n; struct tm *p; while(1) { system("clear"); time(&n);//獲取日歷時間 p = localtime(&n);//轉換為本地時間 printf("%4d-%d-%d:%d:%d:%d\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec); sleep(1); } return 0; }專業(yè)嵌入式技術實訓,咨詢郭老師QQ754634522 |