C Primer Plus 第十四章 编程练习7

作者在 2013-05-17 12:37:59 发布以下内容
/*
修改程序清单14.14,在从文件中读出每个记录并且显示它时,
允许用户选择删除该记录或修改该记录的内容.
如果删除记录,把空出来的数组空间留给下一个要读入的记录.
要能够改变现有的文件内容,必须使用"r+b"模式,而不是"a+b"模式.
要注意文件指针的定位,以便追加的记录不会覆盖已有的记录.
最简单的方法是对存储在程序内存中的数据做所有的改变,
然后再把最后的信息集写入文件中. 
*/
/*
程序清单14.14 : 把结构内容保存到文件中 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

typedef struct{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
} BOOKS;

void additem(BOOKS *);
void delitem(BOOKS *);
void modifyitem(BOOKS *);
void showitem(BOOKS *);
void showmenu(void);

int main(void)
{
	BOOKS library[MAXBKS] = {0};
    int count = 0;
    int i, filecount;
    FILE * pbooks;
    int size = sizeof(BOOKS);
    int flag;
    char menu;
    
    showmenu();
    
    puts("Press [enter] at the start of a line to stop.");
	printf("Please select: ");
    while((menu = getchar()) != '\n')
    {
    	flag = 0;
    	while(getchar() != '\n')
    		continue;
    		
    	switch(menu){
    		case 'a':additem(library);break;
    		case 'd':delitem(library);break;
    		case 'm':modifyitem(library);break;
    		case 's':showitem(library);break;
    		default:
    			{
    				puts("Invalid option.\n");
    				puts("Press [enter] at the start of a line to stop.");
    				printf("Please select again: ");
    				flag = 1;
    			}
    	}
    	
    	if(flag) continue;
    	
		showmenu();
    	puts("Press [enter] at the start of a line to stop.");
		printf("Please select: ");
    }

    puts("\nBye.");
	return 0;
}

void additem(BOOKS * library)
{
	FILE * pbooks;
	int count = 0;
	int size = sizeof(BOOKS);
	int filecount;
	
	if((pbooks = fopen("book.dat", "a+b")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	rewind(pbooks);
     
    while(count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1)
    {
        if(count == 0)
        	puts("Current contents of book.dat: ");
        printf("%s by %s: $%.2f\n", library[count].title, library[count].author, library[count].value);
        count++;
    }
    filecount = count;
    
    if(count == MAXBKS)
    {
        fputs("The book.dat file is full.", stderr);
        exit(2);
    }
    
    puts("Please add new book titles.");
    puts("Press [enter] at the start of a line to stop.");
    while(count < MAXBKS && gets(library[count].title) != NULL && library[count].title[0] != '\0')
    {
    	printf("Now enter the author.\n");
        gets(library[count].author);
        printf("Now enter the value.\n");
        scanf("%f", &library[count++].value);
        
        while(getchar() != '\n')
        	continue;
        
        if(count < MAXBKS)
        {
        	puts("Enter the next title.");
        	puts("Press [enter] at the start of a line to stop.");
        }
    }
    if(count > 0)
    	fwrite(&library[filecount], size, count - filecount, pbooks);
    else
        printf("The new record is not added.\n");
    
    fclose(pbooks);
}

void delitem(BOOKS * library)
{
	FILE * pbooks;
	int count = 0;
	int size = sizeof(BOOKS);
	int flag = 0;
	char ch, select;
	char null[1] = "\0";
	int i;
	
	if((pbooks = fopen("book.dat", "rb")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	rewind(pbooks);
     
    while(count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1)
    {
    	flag++;
        if(count == 0)
        	puts("Current contents of book.dat: ");
        printf("%d : %s by %s: $%.2f\n", count + 1, library[count].title, library[count].author, library[count].value);
        count++;
    }
    
    fclose(pbooks);
    
	if(!flag)
	{
		puts("No record!");
		return;
	}
	
	if((pbooks = fopen("book.dat", "w+b")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	
	puts("Press [enter] at the start of a line to cancel.");
	printf("Please select:");
	while((ch = getchar()) != '\n')
	{
		while(getchar() != '\n')
			continue;
			
		select = ch - '0';
		
		if(--select > count)
		{
			fprintf(stderr, "Without this record.\n\n");
			puts("Press [enter] at the start of a line to cancel.");
			printf("Please select again:");
			continue;
		}
		
		strcpy(library[select].title, null);
		strcpy(library[select].author, null);
		library[select].value = 0;
		printf("Has been deleted : %d\n", select + 1);
		
		
		
		puts("Press [enter] at the start of a line to cancel.");
		printf("Please select:");
		
	}

	for(i = 0; i < count; i++)
    {
    	if(library[i].value != 0)
    	fwrite(&library[i], size, 1, pbooks);
	}
	fclose(pbooks);

}

void modifyitem(BOOKS * library)
{
	FILE * pbooks;
	int count = 0;
	int size = sizeof(BOOKS);
	int flag = 0;
	char ch, select;
	char null[1] = "\0";
	int i;
	
	if((pbooks = fopen("book.dat", "rb")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	rewind(pbooks);
     
    while(count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1)
    {
    	flag++;
        if(count == 0)
        	puts("Current contents of book.dat: ");
        printf("%d : %s by %s: $%.2f\n", count + 1, library[count].title, library[count].author, library[count].value);
        count++;
    }
    
    fclose(pbooks);
    
    if((pbooks = fopen("book.dat", "w+b")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	puts("Press [enter] at the start of a line to cancel.");
	printf("Please select:");
	while((ch = getchar()) != '\n')
	{
		while(getchar() != '\n')
			continue;
			
		select = ch - '0';
		
		if(--select > count)
		{
			fprintf(stderr, "Without this record.\n\n");
			puts("Press [enter] at the start of a line to cancel.");
			printf("Please select again:");
			continue;
		}
		printf("Now enter the new title.\n");
        gets(library[select].title);
		printf("Now enter the new author.\n");
        gets(library[select].author);
        printf("Now enter the new value.\n");
        scanf("%f", &library[select].value);
        
		printf("Has been modify : %d\n", select + 1);
		
		while(getchar() != '\n')
			continue;
		
		puts("Press [enter] at the start of a line to cancel.");
		printf("Please select:");
		
	}

	for(i = 0; i < count; i++)
    {
    	if(library[i].value != 0)
    	fwrite(&library[i], size, 1, pbooks);
	}
	fclose(pbooks);
}

    
    void showitem(BOOKS * library)
{
	FILE * pbooks;
	int count = 0;
	int size = sizeof(BOOKS);
	int flag = 0;
	
	if((pbooks = fopen("book.dat", "rb")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}
	rewind(pbooks);
     
    while(count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1)
    {
    	flag++;
        if(count == 0)
        	puts("Current contents of book.dat: ");
        printf("%s by %s: $%.2f\n", library[count].title, library[count].author, library[count].value);
        count++;
    }
    fclose(pbooks);
    
	if(!flag)
		puts("No record!");
		
    printf("\n");
    
}
				 

void showmenu(void)
{
	puts("----------------------------------------------\n");
	puts("	a). ADDITEM       d). DELITEM\n");
	puts("	m). MODIFYITEM    s). SHOWITEM\n");
	puts("----------------------------------------------");
}
文章评论,共0条
游客请输入验证码
浏览39542次