You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define SIZE 50
void del ( ) {
system ( " cls " ) ;
struct TX {
char name [ 10 ] ;
char jiguan [ 20 ] ;
char d1 [ 20 ] , d2 [ 20 ] ;
char ema [ 30 ] ;
} inf [ SIZE ] ;
FILE * fp ;
int i , j , flag = 1 ;
char a [ 20 ] , n ;
fp = fopen ( " date.dat " , " rb " ) ; //以只读的方式打开文件
printf ( " \t \t \t \t \t \t 请输入要删除的联系人: " ) ;
scanf ( " %s " , a ) ;
for ( i = 0 ; fread ( & inf [ i ] , sizeof ( struct TX ) , 1 , fp ) = = 1 ; i + + ) { //从文件中读取信息
if ( strcmp ( a , inf [ i ] . name ) = = 0 ) flag = 0 ; //比较
}
if ( flag = = 1 ) {
printf ( " \t \t \t \t \t \t 输入错误,此联系人不存在! \n \n \n \n " ) ;
} if ( flag = = 0 ) {
printf ( " \t \t \t \t \t \t 确定删除请输入Y, 否则输入N: " ) ;
getchar ( ) ;
scanf ( " %c " , & n ) ;
if ( n = = ' N ' ) printf ( " \t \t \t \t \t \t 删除失败! \n \n \n " ) ;
if ( n = = ' Y ' ) {
fp = fopen ( " date.dat " , " wb+ " ) ; //打开新二进制文件进行读,写 ,将原有信息覆盖。(替换掉原文件)
for ( j = 0 ; j < i ; j + + ) {
if ( strcmp ( a , inf [ j ] . name ) ! = 0 ) fwrite ( & inf [ j ] , sizeof ( struct TX ) , 1 , fp ) ; //将除了要删除的姓名外的信息写入文件;
} printf ( " \t \t \t \t \t \t 删除成功! \n \n \n \n " ) ;
} else
printf ( " \t \t \t \t \t \t 输入错误! \n \n \n \n " ) ;
}
fclose ( fp ) ;
}