⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 add.c

📁 A simple bibliotech archive coded in C. A real exercise with fopen write and the other base functio
💻 C
字号:
#include "head.h"void  usage() { fprintf (stdout,"\n\nAdd program to the biblioteca.db file\n"                 "Usage       : Required -t -a -e -n -c -p -f \n"                 "Flag info   : -t Title of the book\n"                 "              -a Author of the book\n"                 "              -e Editor of the book\n"                 "              -n Number of pages\n"                 "              -c Cost of the book\n"                 "              -p Position of the book\n"                 "              -f Database file to elaborate\n\n"); } int  load(char *nomefile,struct libro v[]){ FILE *f; int k=0,i=0; if((f=fopen(nomefile,"r"))==NULL){ fprintf(stdout,"[*] I can't load the database file [*]\n");                                    return -1;                                  } while (fscanf(f,"%s%s%s%d%f\n",v[k].titolo,v[k].autori,&v[k].editore,&v[k].num_pagine,&v[k].costo) !=EOF) k++; fflush(f); fclose(f); return 0;}int storage_mem(int position,struct libro ehi,struct libro v[]){  int i ;  if (position>MAX_BOOKS) {fprintf(stdout,"The position you have selected is too high for our database file");return -1;}  for (i=0;i<MAX_BOOKS;i++)  	{ 	 if 	(strncmp(ehi.titolo,v[i].titolo,(sizeof(ehi.titolo)))==0    &&     		 strncmp(ehi.autori,v[i].autori,(sizeof(ehi.autori)))==0    &&     		 strncmp(ehi.editore,v[i].editore,(sizeof(ehi.editore)))==0 &&     		 ehi.num_pagine==v[i].num_pagine && (ehi.costo==v[i].costo))     			{ fprintf  (stdout,"[*] The book you have enter is present on our file at position %d Please enter another one [*]\n" ,i);        		  return -1;			}	} if (v[position].titolo    !='\0' &&      v[position].autori     !='\0' &&      v[position].editore    !='\0' &&      v[position].num_pagine !='\0'&&     v[position].costo      !='\0' )		{    		fprintf (stdout,"[*] The position %d isn't free position please erease it before writing on [*]\n",position);    		return -1;		} v[position]=ehi; return 0;}int  add(char *nome,struct libro passare,int position) { FILE *f;char *risp;int i,min,max;if (load(nome,books)==-1) return -1;fprintf (stdout,"[*] Load the database %s file [*]\n",nome);if (storage_mem( position,passare,books) ==-1) return -1;fprintf (stdout,"[*] Saved in memory your entries [*]\n");fprintf (stdout,"[*] Saving on your database file [*]\n");if((f=fopen(nome,"r+"))==NULL){ fprintf(stdout,"[*] I can't load the database file [*]\n");                                    return -1;                                  } for (i=0;i<MAX_BOOKS;i++) fprintf (f,"%s %s %s %d %f\n", books[i].titolo,books[i].autori,books[i].editore,books[i].num_pagine,books[i].costo);fclose(f);return 0;}int main(int argc , char * argv[]){int position;char *nomefile;struct libro copia;char optchar;	if (argc<14 )		{		usage();		return -1;		}	else 		{		while ((optchar=getopt(argc,argv,"t:a:e:n:c:p:f:"))!=EOF) 			{			switch(optchar) 				{         	                case 't' :					{					if (strlen(optarg)>MAXLENGTH)  					{					fprintf (stdout,"[*] Yor title length is over the fixed length if you want to change edit head.h [*]\n");					return -1;					}					strncpy(copia.titolo,optarg,MAXLENGTH);					}				break;				case 'a' :					{					if (strlen(optarg)>MAXLENGTH)		                        {                                        fprintf (stdout,"[*] Yor author length is over the fixed length if you want to change edit head.h [*]\n");                                        return -1;                                        }					strncpy(copia.autori,optarg,MAXLENGTH);					}				break;				case 'e' :					{					if (strlen(optarg)>MAXLENGTH)		                        {                                        fprintf (stdout,"[*] Yor editor length is over the fixed length if you want to change edit head.h [*]\n");                                        return -1;                                        }					strncpy(copia.editore,optarg,MAXLENGTH);					}									break;				case 'n' :					{					copia.num_pagine=atoi(optarg);					}				break;								case 'c' :					{					copia.costo=atof(optarg);					}					break;                               				case 'p' : 					{					position=atoi(optarg);					if (position>MAX_BOOKS|| position<0)					{				        fprintf (stdout,"[*] The position is over the MAX position fixed (%d) if you want to change edit head.h [*]\n",MAX_BOOKS);                                        return -1;					}					}				break;				case 'f' :				{				nomefile=malloc(strlen(optarg));				strncpy(nomefile,optarg,strlen(optarg));				}								}			}				}			if (add(nomefile,copia,position) ==-1) return -1;return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -