C语言文件管理系统:创建、打开、删除和关闭文件
#include <stdio.h> #include <stdlib.h> #include <string.h>
typedef struct ufd { char filename[10]; int opening; struct ufd *next; } *link;
typedef struct afd { char filename[10]; int opening; struct afd *next; } *linkt;
typedef struct mfd { char username[10]; linkt next; } *linkf;
linkf mfd[5]; linkt afd, p3, q3; link ufd, p2, q2; char x[10]; int t, r, l, i, k, n, s, m, j, il;
void print() { for (i = 0; i < n; i++) { printf("%s", mfd[i]->username); p2 = mfd[i]->next; while (p2) { printf("%s", ufd->filename); printf("%s%d%d", p2->filename, p2->opening, p2->length); p2 = p2->next; } } if (i > 0) { p3 = afd; while (p3) { printf("%s%d", afd->filename, afd->opening); printf("%s%d", p3->filename, p3->opening); p3 = p3->next; } } }
void pcreat() { printf("create name:\n"); scanf("%s", x); q2 = ufd; while (q2->next && (strcmp(q2->filename, x) != 0)) q2 = q2->next; if (q2->next == NULL) { p2 = (linkt)malloc(sizeof(struct ufd)); q2->next = p2; strcpy(p2->filename, x); printf("create code=\n"); scanf("%d", &m); p2->opening = 0; p2->length = m; p2->next = NULL; } else printf("the file is exist\n"); }
void popen() { p2 = ufd; printf("input filename=\n"); scanf("%s", x); while (p2->next && (strcmp(p2->filename, x) != 0)) { p2 = p2->next; if (strcmp(p2->filename, x) == 0) if (i) q3 = q3->next; } if (strcmp(q3->filename, x) == 0) printf("the file is on the afd\n"); else { if (i == 0) p3 = afd; else q3->next = p3; i++; p3 = (linkt)malloc(sizeof(struct afd)); strcpy(p3->filename, x); p3->opening = i; p3->next = NULL; printf("file number=%d\n", i); } else printf("error"); print(); }
void pdelete() { printf("the filename to delete:"); scanf("%s", x); printf("\n"); p3 = afd; q3 = afd; if (strcmp(p3->filename, x) != 0) { q3 = p3; p3 = p3->next; } else { q3->next = p3->next; free(p3); } p2 = ufd; q2 = ufd; if (strcmp(p2->filename, x) != 0) { while (p2->next) { q2 = p2; p2 = p2->next; } } else { q2->next = p2->next; free(p2); printf("the file is deleted\n"); }
print();
}
void pclose() { int j; printf("input close filename:"); scanf("%s", x); q3 = afd; p3 = q3; j = 0; while (strcmp(q3->filename, x) != 0 && q3->next) { p3 = q3; q3 = q3->next; } if (q3->next || strcmp(q3->filename, x) != 0) { p3->next = q3->next; free(q3); printf("end of close\n"); p3 = afd; while (p3->next) { j++; p3 = p3->next; } if (j == 0) il = 0; printf("afd file number=%d", il); } else printf("error"); print(); }
void pbye() { p2 = ufd; while (p2) { printf("%s%d%d\n", p2->filename, p2->opening, p2->length); p2 = p2->next; } printf("2008-11-20"); t = 1; }
int main() { char a; printf("user number n="); scanf("%d", &n); for (i = 0; i < n; i++) { printf("user mfd and the filename:\n"); scanf("%s", x); mfd[i] = (linkf)malloc(sizeof(struct mfd)); strcpy(mfd[i]->username, x); mfd[i]->next = NULL; printf("one user file numbers k="); scanf("%d", &k); p2 = (linkt)malloc(sizeof(struct ufd)); ufd = p2; for (j = 0; j < k; j++) { printf("input ufd name:"); scanf("%s", x); strcpy(p2->filename, x); p2->opening = 0; p2->length = 0; p2->next = (linkt)malloc(sizeof(struct ufd)); p2 = p2->next; } printf("input user name you want to operate:"); scanf("%s", x); while (strcmp(x, mfd[i]->username) != 0) printf("not find ,input again!"); ufd = mfd[i]->next; il = 0; } while (1) { printf("------------------\n"); printf("1:create the file\n"); printf("2:open the file\n"); printf("3:delete the file\n"); printf("4:bye the file\n"); printf("------------------\n"); a = getchar(); switch (a) { case '1': pcreat(); break; case '2': popen(); break; case '3': pdelete(); break; case '4': pbye(); break; default: break; } } return 0;
原文地址: https://www.cveoy.top/t/topic/f1wI 著作权归作者所有。请勿转载和采集!