Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- const char* locations[] = {"Bali", "Mali", "Cook islands", "Bahamas", "Iceland"};
- int location_count[] = {0, 0, 0, 0, 0};
- int location_min[] = {2, 5, 5, 5, 5};
- const char* transports[] = {"Airplane", "Ship", "Bus"};
- const char* DATAFILENAME = "data.txt";
- const char* TEMPFILENAME = "data_temp.txt";
- int locations_len = sizeof(locations) / sizeof(locations[0]);
- int transports_len = sizeof(transports) / sizeof(transports[0]);
- char* people[1024][4];
- // enum Transport{Airplane, Ship, Bus};
- // struct PersonInfo{
- // char name[60];
- // char location[20];
- // char phoneNumber[20];
- // char transport[20];
- // // enum Transport transport;
- // };
- void printSeparator()
- {
- printf("\n###############################\n\n");
- }
- int printAllData()
- {
- printSeparator();
- FILE *fp = fopen(DATAFILENAME, "r");
- if(fp == NULL) {
- perror("Unable to open file!"); //TODO: create file if does not exist
- exit(1);
- }
- int chunkIndex = 0;
- char chunk[256];
- while(fgets(chunk, sizeof(chunk), fp) != NULL)
- {
- char *pt;
- pt = strtok (chunk, ",");
- char* personInfo[4];
- int n = 0;
- while (pt != NULL) {
- personInfo[n] = pt;
- pt = strtok(NULL, ",");
- n++;
- }
- printf("%d) name: %s, location: %s, phone number: %s, transport: %s\n", chunkIndex, personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
- ++chunkIndex;
- }
- printSeparator();
- fclose(fp);
- return chunkIndex;
- }
- int getIntUserInput()
- {
- int input;
- char ch;
- while (scanf("%d", &input) != 1)
- {
- while ((ch = getchar()) != '\n')
- {
- putchar(ch);
- }
- printf(" is not a number.\nPlease enter a number!\n");
- }
- return input;
- }
- int chooseLocationNum()
- {
- int selectedLocationNum;
- printf("The available locations are the following:\n");
- printSeparator();
- for (int i = 0; i < locations_len; ++i)
- {
- printf("%d: %s\n", i, locations[i]);
- }
- printSeparator();
- while(true)
- {
- selectedLocationNum = getIntUserInput();
- if (selectedLocationNum >= 0 && selectedLocationNum < locations_len)
- {
- break;
- }
- printf("Invalid number!\nSelect the number of location you need information about!\n");
- printSeparator();
- for (int i = 0; i < locations_len; ++i)
- {
- printf("%d: %s\n", i, locations[i]);
- }
- printSeparator();
- }
- return selectedLocationNum;
- }
- int chooseTransportNum()
- {
- int selectedTransportNum;
- printf("The available transports are the following:\n");
- printSeparator();
- for (int i = 0; i < transports_len; ++i)
- {
- printf("%d: %s\n", i, transports[i]);
- }
- printSeparator();
- while(true)
- {
- selectedTransportNum = getIntUserInput();
- if (selectedTransportNum >= 0 && selectedTransportNum < transports_len)
- {
- break;
- }
- printf("Invalid number!\nSelect the number of location you need information about!\n");
- printSeparator();
- for (int i = 0; i < transports_len; ++i)
- {
- printf("%d: %s\n", i, transports[i]);
- }
- printSeparator();
- }
- return selectedTransportNum;
- }
- int getChosenMenuPoint(void)
- {
- printf("0) exit the program\n");
- printf("1) input data\n");
- printf("2) delete data\n");
- printf("3) modify existing data\n");
- printf("4) list data by location\n");
- printf("5) list all data\n");
- return getIntUserInput();
- }
- void enterNewData()
- {
- const char* personInfo[4];
- char name[60];
- char location[20];
- char phoneNumber[20];
- char transport[20];
- printf("[Enter name]\n");
- getchar();
- scanf("%[^\n]", name);
- personInfo[0] = name;
- printf("[Enter location numer]\n");
- int chosenLocation = chooseLocationNum();
- personInfo[1] = locations[chosenLocation];
- location_count[chosenLocation] += 1;
- printf("[Enter phone number]\n");
- scanf("%s", phoneNumber);
- personInfo[2] = phoneNumber;
- printf("[Enter transport]\n");
- int chosenTransport = chooseTransportNum();
- personInfo[3] = transports[chosenTransport];
- char lineToAdd[1024];
- snprintf(lineToAdd, sizeof(lineToAdd), "%s,%s,%s,%s\n", personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
- FILE *fp;
- fp = fopen(DATAFILENAME, "a");
- if(fp == NULL) {
- perror("Error opening file.");
- exit(1);
- }
- fprintf(fp, "%s", lineToAdd);
- fclose(fp);
- }
- int deleteData(const char* outputText)
- {
- int maxLineIndex = printAllData() - 1;
- if (maxLineIndex == -1)
- {
- return -1;
- }
- printf("%s", outputText);
- int lineIndexToDelete;
- while(true)
- {
- lineIndexToDelete = getIntUserInput();
- if (lineIndexToDelete >= 0 && lineIndexToDelete <= maxLineIndex)
- {
- break;
- }
- printf("Invalid number!\nSelect a valid number!\n");
- }
- FILE *fp1, *fp2;
- char filename[100], c;
- // Open one file for reading
- fp1 = fopen(DATAFILENAME, "r");
- if (fp1 == NULL)
- {
- printf("Cannot open file %s \n", DATAFILENAME);
- exit(1);
- }
- // Open another file for writing
- fp2 = fopen(TEMPFILENAME, "w");
- if (fp2 == NULL)
- {
- printf("Cannot open file %s \n", TEMPFILENAME);
- exit(1);
- }
- int line_index = 0;
- while (true)
- {
- char ch = getc(fp1);
- if (ch == EOF)
- {
- break;
- }
- //except the line to be deleted
- if (line_index != lineIndexToDelete)
- {
- putc(ch, fp2);
- }
- if (ch == '\n')
- {
- line_index++;
- }
- }
- fclose(fp1);
- fclose(fp2);
- remove(DATAFILENAME);
- rename(TEMPFILENAME, DATAFILENAME);
- return 0;
- }
- void modifyData()
- {
- if (deleteData("choose the index of the line you wanna modify!\n") != -1)
- {
- enterNewData();
- }
- else
- {
- printf("there is no data stored yet! Nothing to modify!\n");
- }
- }
- void listData(bool silent_init = false)
- {
- int selectedLocationNum = -1;
- if (!silent_init)
- {
- printf("[Select the number of location you need information about]\n");
- selectedLocationNum = chooseLocationNum();
- printf("selected location is: %s. The following people are stuck there:\n", locations[selectedLocationNum]);
- printSeparator();
- }
- FILE *fp = fopen(DATAFILENAME, "r");
- if(fp == NULL) {
- perror("Unable to open file!"); //TODO: create file if does not exist
- exit(1);
- }
- char chunk[256];
- while(fgets(chunk, sizeof(chunk), fp) != NULL)
- {
- char *pt;
- pt = strtok (chunk, ",");
- char* personInfo[4];
- int n = 0;
- while (pt != NULL) {
- personInfo[n] = pt;
- pt = strtok(NULL, ",");
- n++;
- }
- if (silent_init)
- {
- for (int i = 0; i < 5; ++i)
- {
- if (strcmp(personInfo[1], locations[i]) == 0)
- {
- location_count[i] += 1;
- break;
- }
- }
- }
- else if (strcmp(personInfo[1], locations[selectedLocationNum]) == 0)
- {
- printf("name: %s, location: %s, phone number: %s, transport: %s\n", personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
- }
- }
- if (!silent_init)
- {
- printSeparator();
- }
- fclose(fp);
- }
- void rescue(int location_index)
- {
- if (fork() == 0)
- {
- printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
- exit(0);
- }
- }
- int main()
- {
- if (access(DATAFILENAME, F_OK) == -1) {
- printf("file does not exist.. creating data.txt\n");
- FILE* fp = fopen(DATAFILENAME, "w");
- fclose(fp);
- }
- listData(true); //init number of stuck people per location
- for (int i = 0; i < 5; ++i)
- {
- if (location_count[i] >= location_min[i])
- {
- printf("Let's rescue some people from: %s!\n", locations[i]);
- rescue(i);
- }
- }
- int n = -1;
- while (n = getChosenMenuPoint()) //it will exit if n is 0
- {
- switch (n)
- {
- case 1:
- enterNewData();
- break;
- case 2:
- if (deleteData("choose the index of the line you wanna delete!\n") == -1)
- {
- printf("there is no data stored yet! Nothing to delete!\n");
- }
- break;
- case 3:
- modifyData();
- break;
- case 4:
- listData();
- break;
- case 5:
- printAllData();
- break;
- case 6:
- for (int i = 0; i < 5; ++i)
- {
- printf("%d, ", location_count[i]);
- }
- printf("\n");
- default:
- printf("Please input a valid menu point number!\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement