Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define action argv[1] // Just for making it easy to read :)
- #define ID argv[2]
- #define NAME argv[3]
- #define AGE argv[4]
- #define db_file "db.dat"
- #define db_backup "db.backup"
- #include <stdio.h>
- #include <stdlib.h> //using atoi to convert strings to numbers
- #include <string.h>
- /*FUNCTION PROTOTYPES*/
- int add(char *id,char *name,char *age);
- int delete(int selected_line);
- int edit(int selected_line,char *id,char *name,char *age);
- int list(int line_number);
- int listall();
- int reset();
- void help();
- char ch; // temp variable for reading
- int current_line; // counter for storing the current line
- int selected_line; // stores the number of line to be edited or deleted, used by edit() and delete() functions
- char flag_edited_line =0; // used in edit() function to prevent multiple printing, senses if the line is written before
- int i; // used in the for loops :)
- FILE *fp1,*fp2;
- int main(int argc,char *argv[])
- {
- // this code makes sure there won't be error while reading from non existing file
- if(!(action == NULL)) // Check if there's an action
- {
- if (( fp1=fopen(db_file,"r") )== NULL) //error opening the file , not exists
- {
- fp1 = fopen(db_file,"w");
- fclose(fp1);
- }
- else
- {
- fclose(fp1);
- }
- // Adding new entry
- if (!strcmp(action,"-a") || !strcmp(action,"--add"))
- {
- // checking the data
- if (argc == 5)
- {
- add(ID,NAME,AGE);
- }
- else
- {
- printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --add ID NAME AGE'\n");
- }
- }
- // Deleting existing entry
- else if (!strcmp(action,"-d") || !strcmp(action,"--delete"))
- {
- if(argc == 3)
- {
- // converting string to integer usint atoi
- selected_line = atoi(ID);
- // confirm deletion
- printf("DO YOU REALLY WANT TO DELETE THIS ENTRY? [Y/N]\n");
- if (getchar() == 'Y')
- {
- // deletion confirmed
- if (!delete(selected_line))
- printf("DELETED SUCESSFULLY\n");
- else
- printf("SOMETHING IS WRONG. DELETION CANCELED\n");
- }
- }
- else
- {
- printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --delete NUMBER_OF_LINE'\n");
- }
- }
- //list all entries
- else if (!strcmp(action,"-la") || !strcmp(action,"--listall"))
- {
- listall();
- }
- // Listing specific entry
- else if (!strcmp(action,"-l") || !strcmp(action,"--list"))
- {
- if(argc == 3)
- {
- selected_line = atoi(ID);
- list(selected_line);
- }
- else
- {
- printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --list NUMBER_OF_LINE'\n");
- }
- }
- // Listing all entries
- else if (!strcmp(action,"-h") || !strcmp(action,"--help"))
- {
- help();
- }
- // Edit specific entry
- else if (!strcmp(action,"-e") || !strcmp(action,"--edit"))
- {
- if(argc == 6)
- {
- // converting string to integer usint atoi
- selected_line = atoi(argv[2]);
- if (!edit(selected_line,argv[3],argv[4],argv[5]))
- printf("EDITED SUCESSFULLY\n");
- else
- printf("SOMETHING IS WRONG. EDIT IS CANCELED\n");
- }
- else
- {
- printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --edit NUMBER_OF_LINE ID NAME AGE'\n");
- }
- }
- // RESET for the data :)
- else if (!strcmp(action,"-r") || !strcmp(action,"--reset"))
- {
- reset();
- }
- // Not valid action
- else
- {
- printf("islam: WRONG ACTION \nTRY 'islam --help' FOR MORE INFORATION.\n");
- }
- }
- else // Missing action
- {
- printf("islam: MISSING ACTION \nTRY 'islam --help' FOR MORE INFORATION.\n");
- help();
- }
- return 0;
- }
- /*Functions*/
- int add(char *id,char *name,char *age) //inputs are 3 strings from argv[]
- {
- // checking and opening the file
- fp1 = fopen(db_file,"a+");
- if(fp1 == NULL)
- {
- printf("ERROR: PROBLEM OPENING THE FILE!\n");
- return 0; //error
- }
- //Printing the line in the file
- fprintf(fp1,"%s,%s,%s\n",id,name,age);
- // Sucess Print
- printf("YOU'VE ADDED ENTRY SUCESSFULLY:\n%s,%s,%s\n",id,name,age);
- fclose(fp1);
- return 0;
- }
- /*********************************************************************************************************/
- int delete(int selected_line)
- {
- fp1 = fopen(db_file,"r");
- fp2 = fopen(db_backup,"w");
- current_line=1;
- if(fp1==NULL || fp2==NULL) // don't run if one of the files didn't open
- {
- printf("ERROR: PROBLEM OPENING THE FILE!\n");//error
- }
- while((ch=getc(fp1))!=EOF)
- {
- // this will increment the current line counter every time it detects \n
- if(ch=='\n')
- current_line++;
- // if not the deleted line keep copying :)
- if(current_line != (selected_line))
- {
- putc(ch,fp2);
- }
- }
- //closing files
- if(fclose(fp2)==EOF)
- printf("ERROR CLOSING fp2\n");
- if(fclose(fp1)==EOF)
- printf("ERROR CLOSING fp1\n");
- // removing the old file and rename the new file
- if (remove(db_file)==0)//check if file removed, remove returns 0 if everything went well
- {
- rename(db_backup,db_file);
- }
- printf("DONE ADDING\n");
- return 0;
- }
- /*********************************************************************************************************/
- int edit(int selected_line,char *id,char *name,char *age)
- {
- fp1 = fopen(db_file,"r");
- fp2 = fopen(db_backup,"w");
- current_line=1;
- flag_edited_line =0; // intaially sets the flag to false every time the function runs :)
- if(fp1==NULL || fp2==NULL) // don't run if one of the files didn't open
- {
- printf("ERROR: PROBLEM OPENING THE FILE!\n");//error
- }
- while((ch=getc(fp1))!=EOF)
- {
- // if not the deleted line keep copying :)
- if(current_line != (selected_line))
- {
- putc(ch,fp2);
- }
- else
- {
- if(flag_edited_line == 0)
- {
- fprintf(fp2, "%s,%s,%s\n", id,name,age);
- }
- flag_edited_line = 1; //sets the flag true to
- }
- // this will increment the current line counter every time it detects \n
- if(ch=='\n')
- current_line++;
- }
- //closing files
- fclose(fp2);
- fclose(fp1);
- // removing the old file and rename the new file
- int ret = remove(db_file); //check if file removed, remove returns 0 if everything went well
- if (ret==0)
- {
- rename(db_backup,db_file);
- }
- printf("DONE EDITING\n");
- return 0;
- }
- /*********************************************************************************************************/
- int listall()
- {
- fp1 = fopen(db_file,"r");
- printf("-----------------------------\nLine No. :: ID , Name , Age\n-----------------------------\n");
- current_line=1;
- printf("1 :: ");
- while((ch=getc(fp1))!=EOF)
- {
- printf("%c",ch);
- if(ch=='\n')
- {
- current_line++;
- printf("%d :: ",current_line);
- }
- }
- if(current_line==1)
- printf("----- NO DATA FOUND :/ -----\n");
- printf("\r-----------------------------\n"); // \r tricky :D
- return 0;
- }
- int list(int line_number)
- {
- if(line_number==0)
- {
- printf("Enter a valid number\n");
- }
- else
- {
- fp1 = fopen(db_file,"r");
- current_line=1;
- printf("Entry Details: ID, Name, Age\n");
- while((ch=getc(fp1))!=EOF)
- {
- // if it's the selected line print it :)
- if(current_line == (selected_line))
- {
- printf("%c",ch);
- }
- // this will increment the current line counter every time it detects \n
- if(ch=='\n')
- current_line++;
- }
- fclose(fp1);
- }
- return 0;
- }
- /*********************************************************************************************************/
- int reset()
- {
- printf("ARE YOU SURE YOU WANT TO ERASE ALL THE DATA [Y/n]?\n");
- ch = getchar();
- if(ch=='Y')
- {
- remove(db_file);
- fp1 = fopen(db_file,"w");
- fclose(fp1);
- }
- return 0;
- }
- void help()
- {
- printf("#######################################################\n#######################################################\nWelcome to isdb : Islam Database Program Version 0.1\nThe program is just for test,\n feel free to contact me on [email protected]\nIt's really easy to use this program, You've 6 main commands (add , edit , delete , list , reset, help)\n1- ADD ENTRY:\n\tSyntax: ./isdb -a [--add] ID NAME AGE \n2- DELETE ENTRY *Require confirmation :\n\tSyntax: ./isdb -d [--delete] NUMBER_OF_LINE \n3- EDIT ENTRY :\n\tSyntax: ./isdb -e [--edit] NUM_OF_LINE ID NAME AGE \n4- LIST SPECIFIC ENTRY :\n\tSyntax: ./isdb -l [--list] NUM_OF_LINE\n5- LIST ALL ENTRIES:\n\tSyntax: ./isdb -la [--listall]\n6- RESET DATABASE (REMOVE ALL ENTRIES) *Require confirmation:\n\tSyntax: ./isdb -r [--reset]\n7- SHOW HELP:\n\tSyntax: ./isdb -h [--help]\n#######################################################\n#######################################################\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment