1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #define len sizeof(struct Student) char conststring[]=" 团支部书记、委员会满意度测评表.docx"; struct Student { int num; char name[1024]; struct Student *next; }; int n; struct Student *Creat(void) { struct Student *head,*p1,*p2; n=0; p1=p2=(struct Student *)malloc(len); scanf("%d %s",&p1->num,p1->name); head=NULL; while(p1->num!=0) { n+=1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Student *)malloc(len); scanf("%d %s",&p1->num,p1->name); } p2->next=NULL; return head; } int main() { struct Student *pt; pt=Creat(); DIR *d = opendir("./"); struct dirent *dirp; struct Student *p; p = pt; if (d) { while (1) { dirp = readdir(d); if (strstr(dirp->d_name, ".docx")) { if(p != NULL) { char oldName[1024]; strcpy(oldName, (dirp)->d_name); char newName[256]; sprintf(newName, "%s%s", p->name, conststring); if (rename(oldName,newName) == 0) { printf("文件 %s 已重命名为 %s\n", oldName, newName); } else { perror("文件重命名失败"); } p = p->next; } } } } else perror("打开文件失败"); system("pause"); return 0; }
|