AnonymousEng

CVS Database (add,edit,delete,list all,list) program

Sep 5th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.76 KB | None | 0 0
  1. #define action argv[1] // Just for making it easy to read :)
  2. #define ID argv[2]
  3. #define NAME argv[3]
  4. #define AGE argv[4]
  5.  
  6. #define db_file "db.dat"
  7. #define db_backup "db.backup"
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h> //using atoi to convert strings to numbers
  11. #include <string.h>
  12.  
  13. /*FUNCTION PROTOTYPES*/
  14. int add(char *id,char *name,char *age);
  15. int delete(int selected_line);
  16. int edit(int selected_line,char *id,char *name,char *age);
  17. int list(int line_number);
  18. int listall();
  19. int reset();
  20. void help();
  21.  
  22.  
  23. char ch;                    // temp variable for reading
  24. int current_line;           // counter for storing the current line
  25. int selected_line;          // stores the number of line to be edited or deleted, used by edit() and delete() functions
  26.  
  27.  
  28.  
  29. char flag_edited_line =0;   // used in edit() function to prevent multiple printing, senses if the line is written before
  30. int i;                      // used in the for loops :)
  31.  
  32.  
  33. FILE *fp1,*fp2;
  34. int main(int argc,char *argv[])
  35. {
  36.    
  37.     // this code makes sure there won't be error while reading from non existing file
  38.  
  39.  
  40.     if(!(action == NULL)) // Check if there's an action
  41.     {
  42.         if (( fp1=fopen(db_file,"r") )== NULL) //error opening the file , not exists
  43.         {
  44.             fp1 = fopen(db_file,"w");
  45.             fclose(fp1);
  46.         }
  47.         else
  48.         {
  49.             fclose(fp1);
  50.         }
  51.  
  52.  
  53.  
  54.         // Adding new entry
  55.         if (!strcmp(action,"-a") || !strcmp(action,"--add"))
  56.         {
  57.             // checking the data
  58.             if (argc == 5)
  59.             {
  60.                 add(ID,NAME,AGE);
  61.             }
  62.             else
  63.             {
  64.                 printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --add ID NAME AGE'\n");
  65.             }
  66.         }
  67.  
  68.  
  69.         // Deleting existing entry
  70.         else if (!strcmp(action,"-d") || !strcmp(action,"--delete"))
  71.         {  
  72.                 if(argc == 3)
  73.                 {
  74.  
  75.                     // converting string to integer usint atoi
  76.                     selected_line = atoi(ID);
  77.  
  78.                     // confirm deletion
  79.                     printf("DO YOU REALLY WANT TO DELETE THIS ENTRY? [Y/N]\n");
  80.                     if (getchar() == 'Y')
  81.                     {
  82.                         // deletion confirmed
  83.                         if (!delete(selected_line))
  84.                         printf("DELETED SUCESSFULLY\n");
  85.                         else
  86.                         printf("SOMETHING IS WRONG. DELETION CANCELED\n");
  87.                     }
  88.  
  89.                    
  90.                 }
  91.                 else
  92.             {
  93.                 printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --delete NUMBER_OF_LINE'\n");
  94.             }
  95.         }
  96.  
  97.  
  98.         //list all entries
  99.         else if (!strcmp(action,"-la") || !strcmp(action,"--listall"))
  100.         {
  101.             listall();
  102.         }
  103.  
  104.  
  105.         // Listing specific entry
  106.         else if (!strcmp(action,"-l") || !strcmp(action,"--list"))
  107.         {
  108.             if(argc == 3)
  109.             {
  110.                 selected_line = atoi(ID);
  111.                 list(selected_line);
  112.             }
  113.             else
  114.             {
  115.                 printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --list NUMBER_OF_LINE'\n");   
  116.             }
  117.         }
  118.  
  119.  
  120.         // Listing all entries
  121.         else if (!strcmp(action,"-h") || !strcmp(action,"--help"))
  122.         {
  123.             help();
  124.         }
  125.  
  126.         // Edit specific entry
  127.         else if (!strcmp(action,"-e") || !strcmp(action,"--edit"))
  128.         {
  129.             if(argc == 6)
  130.                 {
  131.  
  132.                     // converting string to integer usint atoi
  133.                     selected_line = atoi(argv[2]);
  134.  
  135.                     if (!edit(selected_line,argv[3],argv[4],argv[5]))
  136.                         printf("EDITED SUCESSFULLY\n");
  137.                     else
  138.                         printf("SOMETHING IS WRONG. EDIT IS CANCELED\n");
  139.                 }
  140.                 else
  141.             {
  142.                 printf("ERROR: MISSING DATA. CAN'T ADD ENTRY.\nDATA REQUIRED: 'islam --edit NUMBER_OF_LINE ID NAME AGE'\n");
  143.             }
  144.         }
  145.  
  146.  
  147.  
  148.  
  149.         // RESET for the data :)
  150.         else if (!strcmp(action,"-r") || !strcmp(action,"--reset"))
  151.         {
  152.             reset();
  153.         }
  154.  
  155.  
  156.  
  157.  
  158.         // Not valid action
  159.         else
  160.         {
  161.             printf("islam: WRONG ACTION \nTRY 'islam --help' FOR MORE INFORATION.\n");
  162.         }
  163.  
  164.  
  165.     }
  166.     else // Missing action
  167.     {
  168.         printf("islam: MISSING ACTION \nTRY 'islam --help' FOR MORE INFORATION.\n");
  169.         help();
  170.     }
  171.     return 0;
  172. }
  173.  
  174.  
  175. /*Functions*/
  176. int add(char *id,char *name,char *age) //inputs are 3 strings from argv[]
  177. {
  178.                 // checking and opening the file
  179.                 fp1 = fopen(db_file,"a+");
  180.                 if(fp1 == NULL)
  181.                 {
  182.                             printf("ERROR: PROBLEM OPENING THE FILE!\n");
  183.                             return 0; //error
  184.                 }
  185.  
  186.                 //Printing the line in the file
  187.                 fprintf(fp1,"%s,%s,%s\n",id,name,age);
  188.  
  189.                 // Sucess Print
  190.                 printf("YOU'VE ADDED ENTRY SUCESSFULLY:\n%s,%s,%s\n",id,name,age);
  191.  
  192.                 fclose(fp1);
  193.    
  194.     return 0;
  195. }
  196.  
  197.  
  198. /*********************************************************************************************************/
  199.  
  200. int delete(int selected_line)
  201. {
  202.         fp1 = fopen(db_file,"r");
  203.         fp2 = fopen(db_backup,"w");
  204.  
  205.         current_line=1;
  206.  
  207.         if(fp1==NULL || fp2==NULL) // don't run if one of the files didn't open
  208.         {
  209.             printf("ERROR: PROBLEM OPENING THE FILE!\n");//error
  210.         }
  211.  
  212.         while((ch=getc(fp1))!=EOF)
  213.         {
  214.             // this will increment the current line counter every time it detects \n
  215.             if(ch=='\n')
  216.                 current_line++;
  217.  
  218.             // if not the deleted line keep copying :)
  219.             if(current_line != (selected_line))
  220.             {
  221.                 putc(ch,fp2);
  222.             }
  223.         }
  224.  
  225.         //closing files
  226.         if(fclose(fp2)==EOF)
  227.             printf("ERROR CLOSING fp2\n");
  228.         if(fclose(fp1)==EOF)
  229.             printf("ERROR CLOSING fp1\n");
  230.  
  231.         // removing the old file and rename the new file
  232.          
  233.         if (remove(db_file)==0)//check if file removed, remove returns 0 if everything went well
  234.         {
  235.             rename(db_backup,db_file);
  236.         }
  237.  
  238.         printf("DONE ADDING\n");
  239.  
  240.         return 0;
  241.  
  242. }
  243.  
  244. /*********************************************************************************************************/
  245.  
  246. int edit(int selected_line,char *id,char *name,char *age)
  247. {
  248.         fp1 = fopen(db_file,"r");
  249.         fp2 = fopen(db_backup,"w");
  250.         current_line=1;
  251.         flag_edited_line =0; // intaially sets the flag to false every time the function runs :)
  252.  
  253.         if(fp1==NULL || fp2==NULL) // don't run if one of the files didn't open
  254.         {
  255.             printf("ERROR: PROBLEM OPENING THE FILE!\n");//error
  256.         }
  257.  
  258.  
  259.         while((ch=getc(fp1))!=EOF)
  260.         {
  261.             // if not the deleted line keep copying :)
  262.             if(current_line != (selected_line))
  263.             {
  264.                 putc(ch,fp2);
  265.             }
  266.             else
  267.             {
  268.                 if(flag_edited_line == 0)
  269.                 {
  270.                         fprintf(fp2, "%s,%s,%s\n", id,name,age);
  271.                 }
  272.  
  273.                 flag_edited_line = 1; //sets the flag true to
  274.             }
  275.  
  276.  
  277.             // this will increment the current line counter every time it detects \n
  278.             if(ch=='\n')
  279.                 current_line++;
  280.         }
  281.  
  282.         //closing files
  283.         fclose(fp2);
  284.         fclose(fp1);
  285.  
  286.         // removing the old file and rename the new file
  287.         int ret = remove(db_file); //check if file removed, remove returns 0 if everything went well
  288.         if (ret==0)
  289.         {
  290.             rename(db_backup,db_file);
  291.         }
  292.  
  293.         printf("DONE EDITING\n");
  294.         return 0;
  295.  
  296. }
  297.  
  298. /*********************************************************************************************************/
  299.  
  300.  
  301. int listall()
  302. {
  303.     fp1 = fopen(db_file,"r");
  304.     printf("-----------------------------\nLine No. :: ID ,  Name , Age\n-----------------------------\n");
  305.  
  306.     current_line=1;
  307.     printf("1 :: ");       
  308.     while((ch=getc(fp1))!=EOF)
  309.     {      
  310.                 printf("%c",ch);
  311.                 if(ch=='\n')
  312.                 {
  313.                     current_line++;
  314.                     printf("%d :: ",current_line);
  315.                 }
  316.     }
  317.             if(current_line==1)
  318.                 printf("----- NO DATA FOUND :/ -----\n");
  319.  
  320.             printf("\r-----------------------------\n"); // \r tricky :D
  321.     return 0;
  322. }
  323.  
  324.  
  325. int list(int line_number)
  326. {
  327.     if(line_number==0)
  328.     {
  329.         printf("Enter a valid number\n");  
  330.     }
  331.     else
  332.     {
  333.             fp1 = fopen(db_file,"r");
  334.             current_line=1;
  335.    
  336.             printf("Entry Details: ID, Name, Age\n");
  337.             while((ch=getc(fp1))!=EOF)
  338.             {
  339.                
  340.  
  341.                 // if it's the selected line print it :)
  342.                 if(current_line == (selected_line))
  343.                 {
  344.                     printf("%c",ch);
  345.                 }
  346.  
  347.                 // this will increment the current line counter every time it detects \n
  348.                 if(ch=='\n')
  349.                     current_line++;
  350.             }
  351.             fclose(fp1);
  352.     }
  353.    
  354.     return 0;
  355. }
  356.  
  357. /*********************************************************************************************************/
  358.  
  359.  
  360. int reset()
  361. {
  362.     printf("ARE YOU SURE YOU WANT TO ERASE ALL THE DATA [Y/n]?\n");
  363.     ch = getchar();
  364.     if(ch=='Y')
  365.     {
  366.         remove(db_file);
  367.         fp1 = fopen(db_file,"w");
  368.         fclose(fp1);
  369.     }
  370.     return 0;
  371. }
  372.  
  373. void help()
  374. {
  375.     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");
  376. }
Advertisement
Add Comment
Please, Sign In to add comment