简单CD管理器【学习c】




#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

const char* CD_LIST = "cds.txt";
const char* MUSIC_LIST = "musics.txt";
const char* TEMP_FILE = "temp.txt";
const int NOSELECT = -1;
const int MAX_NAME = 30;
const int MAX_LINE = 100;
int mselect = -1;
char mselectcd[100];

void welcome(){
	printf("Welcome to Simple CD Manager\n");
	printf("author : lize\n");
}

void showcdsmenu(){
	printf("please input your choice:\n");
	printf("c     create a new cd\n");
	printf("l     list all cds\n");
	printf("s     select a cd\n");
	printf("q     quit\n");
}

void showmusicsmenu(){
	printf("you selected %s\n" , mselectcd);
	printf("please input your choice:\n");
	printf("a     add a new music\n");
	printf("r     remove a music\n");
	printf("d     delete the cd\n");
	printf("l     list musics in the cd \n");
	printf("b     back to cd lists\n");
	printf("q     quit\n");
}

void ffllushstdin(){
	int c;
        while ((c=getchar()) != '\n' && c != EOF);
//	setbuf(stdin , NULL);   没啥用。看完当上说是能将流缓冲区的数据清空。可是使用发现不仅没效果,缓冲区的数据却乱了。
}

void getinputc(char* input){
	printf("?");
	scanf(" %c",input);
}

void getinputi(int* input){
	printf("?");
	scanf(" %d",input);
}

void getinputs(char* name , const int length){
	printf("?");
	ffllushstdin();
	fgets(name , length , stdin);
}

void removenline(const char* str){
	char* ptr;
	ptr = str;
	if(*ptr == 0 ) return;
	while(*ptr != 0){
		ptr++;
	}
	ptr--;
	if(*ptr == '\n'){
		*ptr = 0;
	}
}


int getmaxno(const char* name){
	FILE *file;
	char line[MAX_LINE];

	int maxno = 0;
	file=fopen(name,"r");
	if(NULL != file){
		fgets(line,MAX_LINE,file);
		while(!feof(file)){
			sscanf(line," %d\n",&maxno);
			fgets(line,MAX_LINE,file);
		}
		fclose(file);
	}
	return maxno;
}


void searchno(const int no , const char* filename){
	FILE *file;
	int i;
	char line[MAX_LINE];

	file=fopen(filename,"r");
	if(NULL != file){
		fgets(line,MAX_LINE,file);
		while(!feof(file)){
			if(1 == sscanf(line,"%d",&i ) && i == no){
				mselect = no;
				strcpy(mselectcd , line);
				break;
			}
			fgets(line,MAX_LINE,file);
		}
		fclose(file);
	}
	if(mselect == NOSELECT){
		printf("not found!");
	}
}

void quit(){
	exit(EXIT_SUCCESS);
}

void createcd(){
	char name[MAX_NAME];
	FILE *file;
	int no;
	
	printf("please input the name of your cd : \n");
	getinputs(name , MAX_NAME);
	no = getmaxno(CD_LIST) +1 ;
	if(1 == no){
		file = fopen(CD_LIST , "w");
	}else{
		file = fopen(CD_LIST , "a");
	}
	fprintf(file,"%d,%s",no,name);    
	fclose(file);
}

void listcds(){
	FILE* file;
	int no;
	char line[MAX_LINE];

	file = fopen(CD_LIST , "r");
	if(NULL == file ) printf("no cd ! \n");
	else {
		fgets(line,MAX_LINE,file);
		while(!feof(file)){
			printf("%s",line);
			fgets(line,MAX_LINE,file);
		}
		fclose(file);
	}
}

void selectcd(){
	int no;
	printf("please input the no \n");
	getinputi(&no);
	searchno(no,CD_LIST);
}

void cdmanager(){
	char c;
	showcdsmenu();
	getinputc(&c);
	switch(c){
	case 'c':
		createcd();
		break;
	case 'l':
		listcds();
		break;
	case 's':
		selectcd();
		break;
	case 'q':
		quit();
		break;
	default:
		printf("error input\n");
		break;
	}
}

void addmusic(){
	char name[MAX_NAME];
	FILE *file;
	int no;

	printf("please input the name of the music : \n");
	getinputs(name , MAX_NAME);
	no = getmaxno(MUSIC_LIST) +1 ;
	if(1 == no){
		file = fopen(MUSIC_LIST , "w");
	}else{
		file = fopen(MUSIC_LIST , "a");
	}
	fprintf(file,"%d,%d,%s",no,mselect,name);
	fclose(file);
}

void listmusics(){
	FILE* file;
	int no , cd;
	char name[MAX_NAME];
	char line[MAX_LINE];

	file = fopen(MUSIC_LIST , "r");
	if(NULL == file ) printf("no musics ! \n");
	else {
		fgets(line,MAX_NAME,file);
		while(!feof(file)){
			if(3 == sscanf(line, "%d,%d,%s",&no,&cd,name) && cd == mselect)
				printf("%s",line);
			fgets(line,MAX_NAME,file);
		}
		fclose(file);
	}
}

void removemusic(){
	char name[MAX_NAME] , line[MAX_LINE];
	FILE *file , *tempfile;
	int input , no;

	printf("please input the no of the music : \n");
	getinputi(&input);
	file = fopen(MUSIC_LIST , "r");
	tempfile = fopen(TEMP_FILE , "w+");
	fgets(line,MAX_LINE,file);
	while(!feof(file)){
		if(1 == sscanf(line , "%d",&no) && no != input)	
			fprintf(tempfile,"%s",line);
		fgets(line,MAX_LINE,file);
	}
	fclose(file);
	fseek(tempfile , 0 , SEEK_SET);
	file = fopen(MUSIC_LIST , "w");
	fgets(line,MAX_LINE,tempfile);
	while(!feof(tempfile)){
		fprintf(file,"%s",line);
		fgets(line,MAX_LINE,tempfile);
	}
	fclose(file);
	fclose(tempfile);
	unlink(TEMP_FILE);
}

void back(){
	mselect = NOSELECT;
}

void deletecd(){
	char name[MAX_NAME] , line[MAX_LINE];
	FILE *file , *tempfile;
	int input , no;

	file = fopen(CD_LIST , "r");
	tempfile = fopen(TEMP_FILE , "w+");
	fgets(line,MAX_LINE,file);
	while(!feof(file)){
		if(1 == sscanf(line , "%d",&no) && no != mselect)	
			fprintf(tempfile,"%s",line);
		fgets(line,MAX_LINE,file);
	}
	fclose(file);
	fseek(tempfile , 0 , SEEK_SET);
	file = fopen(CD_LIST , "w");
	fgets(line,MAX_LINE,tempfile);
	while(!feof(tempfile)){
		fprintf(file,"%s",line);
		fgets(line,MAX_LINE,tempfile);
	}
	fclose(file);
	fclose(tempfile);
	unlink(TEMP_FILE);
	back();
}



void musicmanager(){
	char c;
	showmusicsmenu();
	getinputc(&c);
	switch(c){
	case 'a':
		addmusic();
		break;
	case 'l':
		listmusics();
		break;
	case 'r':
		removemusic();
		break;
	case 'd':
		deletecd();
		break;
	case 'b':
		back();
		break;
	case 'q':
		quit();
		break;
	default:
		printf("error input\n");
		break;
	}
}


int main(){
	welcome();
	while(1){
		if(NOSELECT == mselect){
			cdmanager();
		}else{
			musicmanager();
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值