📄 file.c
字号:
#define LEN sizeof(INFO)
/* FUNCTION 010 */
/* This function is designed to output the records to a TXT file. */
void output_txt(INFO *p)
{
FILE *fp;
INFO *p_tmp;
char name[15];
extern n;
p_tmp=p;
printf("\nPlease enter a file name to save into\n(within 8 characters):");
scanf("%s",name);
fp=fopen(strcat(name,".txt"),"w+"); /* add the suffix ".txt" */
if(fp==NULL)
{
printf("Cannot open file.\n");
return;
}
else printf("File opened successfully.\n");
fprintf(fp,"\nThere are %d student info records:\n",n);
fprintf(fp,"\nNUMBER NAME GENDER SCHOOL MAJOR SORT\n");
do
{
char MJR[9];
char SCH[9];
switch(p_tmp->school)
{case 1:strcpy(SCH,"Jidian");break;
case 2:strcpy(SCH,"Diankong");break;
case 6:strcpy(SCH,"Shuli");break;
case 7:strcpy(SCH,"Jisuanji");break;}
switch(p_tmp->major)
{case 0:strcpy(MJR,"Zidonghua");break;
case 1:strcpy(MJR,"Dianxin");break;
case 2:strcpy(MJR,"Shuxue");break;
case 3:strcpy(MJR,"Weidianzi");break;
case 4:strcpy(MJR,"Tongxin");break;
case 5:strcpy(MJR,"Jixie");break;
case 6:strcpy(MJR,"Wuli");break;
case 7:strcpy(MJR,"Jisuanji");break; }
fprintf(fp,"%-10s%-22s%c\t%-10s%-11s%c\n",
p_tmp->number,p_tmp->name,p_tmp->gender,SCH,MJR,p_tmp->sort);
p_tmp=p_tmp->next;
}while(p_tmp!=NULL);
printf("\nFile saved successfully!\n");
fclose(fp);
getch();
}
/* FUNCTION 011 */
/* This function is to load data from a binary file to memory. */
void f_load_2(INFO *p)
{
extern n;
INFO *p_new, *p_temp;
FILE *fp;
char name[15];
p_temp=p_new=p;
printf("\nPlease enter the binary file name to load from\n(within 8 characters):");
scanf("%s",name);
if((fp=fopen(strcat(name,".2"),"rb+"))==NULL) /* add the suffix ".2", indicating it is a binary file */
{
printf("Cannot open file.\n");getch();
return;
}
else {printf("\nFile opened successfully!\n");getch();}
n=0;
fread(p_new,LEN,1,fp);
while(!(feof(fp)))
{
p_temp=p_new;
p_new=(INFO *)malloc(LEN);
fread(p_new,LEN,1,fp);
p_temp->next=p_new;
n++;
}
p_temp->next=NULL;
fclose(fp);
printf("\nFile loaded successfully!\n");
getch();
}
/* FUNCTION 012 */
/* This function is to save data from memory to a binary file. */
void f_save_2(INFO *p)
{
FILE * fp;
char name[15];
printf("Please enter the file name to save in\n(within 8 characters):");
scanf("%s",name);
if((fp=fopen(strcat(name,".2"),"wb+"))==NULL)
{
printf("Cannot open file.\n");
return;
}
else printf("\nFile opened successfully!\n");
do
{fwrite(p,LEN,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
printf("\nFile saved successfully!\n");
getch();
}
/* Function 016 */
/* Sub-menu of saving the namelist to file */
void menu_save(void)
{
char choice_save;
extern INFO *data_head;
do
{
clrscr();
printf("\n\t ====== Save list to files ======\n");
printf("\n Do you want to ...\n");
printf("\n\t 1. Save current list to a binary file.");
printf("\n\t 2. Export current list to a TXT report.\n");
printf("\n\t X. Quit to previous menu\n");
printf("\n Please choose one item:");
scanf("%*c%c",&choice_save);
switch(choice_save)
{
case 'x':
case 'X': break;
case '1': clrscr();f_save_2(data_head);back_print();break;
case '2': clrscr();output_txt(data_head);back_print();break;
}
}while(choice_save!='x'&&choice_save!='X');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -