Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #define MAX 100
- #define CLEAR system("clear");
- static unsigned int index = 0;
- struct human{
- int age;
- char name[MAX];
- char country[MAX];
- };
- void new_human(struct human *lib){
- lib = (struct human *)malloc(sizeof(struct human));
- printf("\nName: ");
- fgets(lib[index].name, MAX, stdin);
- printf("Country: ");
- fgets(lib[index].country, MAX, stdin);
- printf("Age: ");
- scanf("%i", &lib[index].age);
- printf("\n");
- index++;
- }
- void save_lib_data(struct human *lib, char *filename){
- FILE *datei = fopen(filename, "a");
- for(index; index>=0; index--){
- fprintf(datei, "\n---------------------------\n");
- fprintf(datei, "Name : %s", lib[index].name);
- fprintf(datei, "Country : %s", lib[index].country);
- fprintf(datei, "Age : %i", lib[index].age);
- fprintf(datei, "-----------------------------\n");
- }
- fclose(datei);
- }
- int main(int argc, char *args[]){
- unsigned short auswahl;
- struct human *lib;
- while(1){
- CLEAR
- printf("\n1) New Human\n");
- printf("2) Save\n");
- printf("3) Quit\n");
- printf("\n: ");
- scanf("%d", &auswahl);
- getc(stdin);
- if(auswahl == 1){
- new_human(lib);
- }
- else if(auswahl == 2){
- save_lib_data(lib, args[1]);
- }
- else if(auswahl == 3){
- break;
- }
- else{
- printf("Fehlerhafte eingabe\n");
- fseek(stdin,0,SEEK_END);
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment