/* 一个简易的用文件存储数据的程序 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct pcb
{
int num; /* 编号 */
char kehu[40]; /* 客户 */
char gongyi[30]; /* 工艺 */
char kebian[50]; /* 客编 */
char beizhu[200]; /* 备注 */
};
int write_pcb(FILE * pf); /* 修改 (edit_file()函数访问此函数) */
int show_file(FILE * pf); /* 显示文件内容 */
int edit_file(FILE * pf); /* 修改、查找、显示 */
FILE * create_file(char * fname); /* 打开 fname 文件 */
int main(void)
{
FILE * pf;
char fname[125] = {'\0'};
while(fname[0] == '\0')
{
printf("Please enter the file name: ");
gets(fname);
}
pf = create_file(fname);
while(edit_file(pf))
continue;
fclose(pf); /* 关闭文件 */
puts("\nThe End!");
system("Pause");
return 0;
}
/* 打开文件 ****************************/
FILE * create_file(char * fname)
{
FILE * pf = NULL;
int ch;
if((pf = fopen(fname, "r+b")) == NULL) /* 文件不存在则创建 */
{
printf("The %s does not exist!\n", fname);
printf("Whether the creation of the document? (Q to quit)\n");
printf("[Y / N]... ");
while((ch = getchar()) != 'y'&& ch != 'Y'
&& ch != 'n' && ch != 'N' )
{
while(getchar() != '\n')
;
printf("[Y / N]... ");
}
while(getchar() != '\n')
;
switch(ch)
{
case 'y':
case 'Y':
if((pf = fopen(fname, "w+b")) == NULL)
{
puts("Failure to create documents!");
system("Pause");
exit(1);
}
break;
case 'n':
case 'N':
puts("Goodbye!");
system("Pause");
exit(1);
break;
default :
puts("Goodbye!");
system("Pause");
exit(1);
break;
}
}
else
printf("%s to open!\n", fname);
return pf;
}
/* 显示文件内容 ******************/
int show_file(FILE * pf)
{
struct pcb temp;
int count = 0;
if(pf == NULL)
{
fprintf(stderr, "Error! [enter]...");
getchar();
exit(1);
}
rewind(pf); /* 回到文件开始处 */
system("CLS");
puts("============== File ==================");
while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
{
++count;
printf("\nId: %d\n kehu: %s\n gongyi%s\n kebian%s\n beizhu%s\n"
,temp.num, temp.kehu, temp.gongyi, temp.kebian, temp.beizhu);
if(count % 3 == 0)
{
system("Pause");
system("CLS");
}
}
if(count == 0)
printf("\nfile is null!\n\n");
else
printf("\n Count: %d\n\n", count);
rewind(pf); /* 回到文件开始处 */
return count;
}
/* 项目写入操作 ****************************/
int write_pcb(FILE * pf)
{
struct pcb temp;
printf("Id: ");
while(scanf("%d", &temp.num) != 1)
{
while(getchar() != '\n')
continue;
printf("Id: ");
}
while(getchar() != '\n')
continue;
printf("kehu: ");
gets(temp.kehu);
printf("gongyi: ");
gets(temp.gongyi);
printf("kebian: ");
gets(temp.kebian);
printf("beizhu: ");
gets(temp.beizhu);
if(fwrite(&temp, sizeof(struct pcb), 1, pf) != 1)
{
fprintf(stderr, "Error! [enter]...\n");
getchar();
exit(1);
}
return 1;
}
/* 编辑文件 **********************************/
int edit_file(FILE * pf)
{
int ch = '\0';
int num;
int i, k;
struct pcb temp;
if(pf == NULL)
{
fprintf(stderr, "Error! [enter]...");
getchar();
exit(1);
}
rewind(pf); /* 回到文件开始处 */
puts("================== Menu ==================\n");
while(ch != 'e' && ch != 'f' && ch != 'a' && ch != 'q' && ch != 's')
{
printf("\na)add e)edit\n"
"f)find s)show\n"
"q)quit\n\n" );
ch = getchar();
if(ch != '\n')
while(getchar() != '\n');
}
system("CLS");
switch(ch)
{
case 'a':
puts("Add ---------------------------\n");
fseek(pf, 0l, SEEK_END); /* 到文件结尾 */
write_pcb(pf);
break;
case 'e':
puts("Edit ---------------------------\n");
printf("Edit Id: ");
while(scanf("%d", &num) != 1)
{
while(getchar() != '\n')
continue;
printf("Edit Id: ");
}
while(getchar() != '\n')
continue;
rewind(pf); /* 回到文件开始处 */
k = 0;
while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
{
if(num == temp.num)
{
k = 1;
i = ftell(pf) - sizeof(struct pcb);
fseek(pf, (long)i, SEEK_SET);
write_pcb(pf);
break;
}
}
if(k < 1)
{
printf("不存在!\n");
system("Pause");
}
break;
case 'f':
puts("Find ---------------------------\n");
printf("Find Id: ");
while(scanf("%d", &num) != 1)
{
while(getchar() != '\n')
continue;
printf("Find Id: ");
}
while(getchar() != '\n')
continue;
rewind(pf); /* 回到文件开始处 */
k = 0;
while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
{
if(num == temp.num)
{
k = 1;
printf("记录存在!\n");
break;
}
}
if(k < 1)
printf("不存在!\n");
system("Pause");
break;
case 's':
show_file(pf);
break;
default :
return 0;
break;
}
rewind(pf); /* 回到文件开始处 */
return 1;
}
—>〉Sun〈<—