Guest User

argus.c

a guest
Sep 25th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define MAX 100
  5. #define CLEAR system("clear");
  6.  
  7. static unsigned int index = 0;
  8.  
  9. struct human{
  10.     int age;
  11.     char name[MAX];
  12.     char country[MAX];
  13. };
  14.  
  15. void new_human(struct human *lib){
  16.    
  17.     lib = (struct human *)malloc(sizeof(struct human));
  18.  
  19.     printf("\nName: ");
  20.     fgets(lib[index].name, MAX, stdin);
  21.            
  22.     printf("Country: ");
  23.     fgets(lib[index].country, MAX, stdin);
  24.    
  25.     printf("Age: ");
  26.     scanf("%i", &lib[index].age);
  27.     printf("\n");
  28.     index++;
  29. }
  30.  
  31.  
  32. void save_lib_data(struct human *lib, char *filename){
  33.     FILE *datei = fopen(filename, "a");
  34.     for(index; index>=0; index--){
  35.         fprintf(datei, "\n---------------------------\n");
  36.         fprintf(datei, "Name     : %s", lib[index].name);
  37.         fprintf(datei, "Country  : %s", lib[index].country);
  38.         fprintf(datei, "Age      : %i", lib[index].age);
  39.         fprintf(datei, "-----------------------------\n");
  40.     }
  41.     fclose(datei);
  42. }
  43.  
  44.  
  45. int main(int argc, char *args[]){
  46.     unsigned short auswahl;
  47.     struct human *lib;
  48.     while(1){
  49.        
  50.         CLEAR
  51.         printf("\n1) New Human\n");
  52.         printf("2) Save\n");
  53.         printf("3) Quit\n");
  54.        
  55.         printf("\n: ");
  56.         scanf("%d", &auswahl);
  57.         getc(stdin);
  58.        
  59.         if(auswahl == 1){
  60.             new_human(lib);
  61.         }
  62.         else if(auswahl == 2){
  63.             save_lib_data(lib, args[1]);  
  64.         }
  65.         else if(auswahl == 3){
  66.             break;
  67.         }
  68.         else{
  69.             printf("Fehlerhafte eingabe\n");
  70.             fseek(stdin,0,SEEK_END);
  71.         }
  72.     }
  73.     printf("\n");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment