自己编写的小型文件管理系统
刚学完c语言,有点兴奋,自己编写了一个小型文件管理系统,漏洞百出,求高手指点.#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Display(void)
{
FILE *fp;
char name[10], ch;
printf("Input the file name:");
scanf("%s",name);
if((fp = fopen(name,"r")) == NULL)
{
printf("Cannot open the file!\n");
exit(0);
}
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
void Copy(void)
{
FILE *in, *out;
char namein[100], nameout[100], ch;
printf("Input the file name:");
scanf("%s", namein);
printf("Input the arm file name:");
scanf("%s", nameout);
if((in = fopen(namein, "rb")) == NULL)
{
printf("Cannot open the file!\n");
exit(0);
}
if((out = fopen(nameout, "wb")) == NULL)
{
printf("Cannot open the arm file!\n");
exit(0);
}
while(!feof(in))
{
ch = fgetc(in);
fputc(ch,out);
}
fclose(in);
fclose(out);
printf("Complete.");
return 0;
}
void Connect(void)
{
FILE *fp1, *fp2;
char ch;
char name1[100], name2[100];
printf("Input the first file:");
scanf("%s", name1);
printf("Input the second file:");
scanf("%s", name2);
if((fp1 = fopen(name1, "rb")) == NULL)
{
printf("Cannot open the first file!");
exit(0);
}
if((fp2 = fopen(name2, "ab")) == NULL)
{
printf("Cannot open the second file!");
exit(0);
}
while(!feof(fp1))
{
ch = fgetc(fp1);
fputc(ch,fp2);
}
printf("Connect OK!");
fclose(fp1);
fclose(fp2);
return 0;
}
void Compare(void)
{
FILE *fp1, *fp2;
char s[100], t[100], name1[100], name2[100];
printf("Input file one:");
scanf("%s", name1);
printf("Input file two:");
scanf("%s", name2);
if((fp1 = fopen(name1, "rb")) == NULL)
{
printf("Cannot open the file one!\n");
exit(0);
}
if((fp2 = fopen(name2, "rb")) == NULL)
{
printf("Cannot open the file two!\n");
exit(0);
}
fread(s, sizeof(FILE), 1, fp1);
fread(t, sizeof(FILE), 1, fp2);
if(strcmp(s,t) == 0)
printf("The two file are equal!\n");
else
printf("The two file are not equal!\n");
fclose(fp1);
fclose(fp2);
return 0;
}
void Delete(void)
{
char name[100];
printf("Input the file name:");
scanf("%s", name);
if(unlink(name) == 0)
printf("delete the file OK!");
printf("\n");
return 0;
}
void Rename(void)
{
char oldname[100], newname[100];
printf("Input the old and the new name:");
scanf("%s%s", oldname, newname);
if(rename(oldname, newname) == 0)
printf("Rename OK!\n");
else printf("Rename fault!\n");
return 0;
}
main()
{
int i;
printf("****************MENU******************\n");
printf(" 0.\tDisplay\tText\tFile\n");
printf(" 1.\tCopy\tFile\n");
printf(" 2.\tConnect\tFile\n");
printf(" 3.\tCompare\tFile\n");
printf(" 4.\tDelete\tFile\n");
printf(" 5.\tRename\tFile\n");
printf(" 6.\tQuit\n");
printf("**************************************\n");
printf("Enter your choice<0~7>:");
scanf("%d", &i);
switch(i)
{
case 0: Display();
case 1: Copy();
case 2: Connect();
case 3: Compare();
case 4: Delete();
case 5: Rename();
case 6: exit(0);
}
}