Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DataFileManagement1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "ctype.h"
- #include "math.h"
- #include "time.h"
- FILE * infoPointer;
- typedef struct data
- {
- char firstName[50];
- char lastName[50];
- char companyName[50];
- char address[50];
- char city[50];
- char county[50];
- char state[50];
- char zip[50];
- char phone1[50];
- char phone2[50];
- char email[50];
- char website[50];
- };
- struct data personData[500];
- void OpenFile();
- void CloseFile();
- void ChooseData();
- void SetData(char string[], int option);
- int main()
- {
- OpenFile();
- ChooseData();
- return 0;
- }
- void OpenFile()
- {
- infoPointer = fopen("C:\\Users\\CraigBreathnach\\Desktop\\GY350 College\\CT103 Computer Programming\\Semester 2\\Week 5\\DataFile1.txt", "r");
- if (infoPointer == NULL)
- {
- printf("Error! The file was not read correctly. Please check that the file path is correct.\n\n");
- }
- else
- {
- printf("Success! The file was read correctly. The file path is correct.\n\n");
- }
- char lines[501];
- for (int i = 0; i <= 1; i++)
- {
- fgets(lines, 87, infoPointer);
- }
- char delimiter[2] = ",";
- char *token;
- int j = 0;
- while (!feof(infoPointer))
- {
- fgets(lines, 501, infoPointer);
- //replaceChar(lines, '\"', ' ');
- if ((token = (strtok(lines, delimiter))) != NULL)
- {
- strcpy(personData[j].firstName, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].lastName, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].companyName, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].address, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].city, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].county, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].state, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].zip, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].phone1, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].phone2, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].email, token);
- }
- if ((token = (strtok(NULL, delimiter))) != NULL)
- {
- strcpy(personData[j].website, token);
- }
- j++;
- }
- CloseFile();
- }
- void CloseFile()
- {
- fclose(infoPointer);
- }
- void ChooseData()
- {
- int chooseData = 0;
- char searchTerm[50];
- printf("Please choose an option to display the required data : \n1. Search By Name. \n2. Search by Company. \n3. Search by City. \n4. Search by County. \n5. Search by State. \n6. Search by ZIP. \n7. Exit Program. \n\n");
- scanf("%d", &chooseData);
- printf("\n");
- switch (chooseData)
- {
- case 1:
- printf("You selected option number 1. \nPlease input a Persons Name to be searched for.\n\nPerson Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- case 2:
- printf("You selected option number 2. \nPlease input a Company Name to be searched for.\n\nCompany Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- case 3:
- printf("You selected option number 3. \nPlease input a City Name to be searched for.\n\nCity Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- case 4:
- printf("You selected option number 4. \nPlease input a County Name to be searched for.\n\nCounty Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- case 5:
- printf("You selected option number 5. \nPlease input a State Name to be searched for.\n\nState Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- case 6:
- printf("You selected option number 6. \nPlease input a ZIP Code to be searched for.\n\nZIP Search Term (No more than 50 characters) : \n\n");
- scanf("%s", searchTerm);
- SetData(searchTerm, chooseData);
- break;
- default:
- printf("You selected option number 7. The program will now end. Goodbye. \n\n");
- exit;
- }
- }
- void SetData(char string[], int option)
- {
- if (option == 1)
- {
- for (int k = 0; k <= 499; k++)
- {
- if ((strstr(_strupr(personData[k].firstName), _strupr(string))) || (strstr(_strupr(personData[k].lastName), _strupr(string))))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[k].firstName, personData[k].lastName, personData[k].companyName, personData[k].address, personData[k].city, personData[k].county,
- personData[k].state, personData[k].zip, personData[k].phone1, personData[k].phone2, personData[k].email, personData[k].website);
- }
- }
- }
- else if (option == 2)
- {
- for (int l = 0; l <= 499; l++)
- {
- if (strstr(_strupr(personData[l].companyName), _strupr(string)))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[l].firstName, personData[l].lastName, personData[l].companyName, personData[l].address, personData[l].city, personData[l].county,
- personData[l].state, personData[l].zip, personData[l].phone1, personData[l].phone2, personData[l].email, personData[l].website);
- }
- }
- }
- else if (option == 3)
- {
- for (int m = 0; m <= 499; m++)
- {
- if (strstr(_strupr(personData[m].city), _strupr(string)))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[m].firstName, personData[m].lastName, personData[m].companyName, personData[m].address, personData[m].city, personData[m].county,
- personData[m].state, personData[m].zip, personData[m].phone1, personData[m].phone2, personData[m].email, personData[m].website);
- }
- }
- }
- else if (option == 4)
- {
- for (int n = 0; n <= 499; n++)
- {
- if (strstr(_strupr(personData[n].county), _strupr(string)))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[n].firstName, personData[n].lastName, personData[n].companyName, personData[n].address, personData[n].city, personData[n].county,
- personData[n].state, personData[n].zip, personData[n].phone1, personData[n].phone2, personData[n].email, personData[n].website);
- }
- }
- }
- else if (option == 5)
- {
- for (int o = 0; o <= 499; o++)
- {
- if (strstr(_strupr(personData[o].state), _strupr(string)))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[o].firstName, personData[o].lastName, personData[o].companyName, personData[o].address, personData[o].city, personData[o].county,
- personData[o].state, personData[o].zip, personData[o].phone1, personData[o].phone2, personData[o].email, personData[o].website);
- }
- }
- }
- else if (option == 6)
- {
- for (int p = 0; p <= 499; p++)
- {
- if (strstr(_strupr(personData[p].zip), _strupr(string)))
- {
- printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
- string, personData[p].firstName, personData[p].lastName, personData[p].companyName, personData[p].address, personData[p].city, personData[p].county,
- personData[p].state, personData[p].zip, personData[p].phone1, personData[p].phone2, personData[p].email, personData[p].website);
- }
- }
- }
- else
- {
- printf("Error! The program will now close. \n\n");
- exit;
- }
- ChooseData();
- }
- /*
- fgetc used to replace a character when reading charater by character.
- For Replacing Characters :
- void replaceChar(char *string, char oldC, char newC)
- {
- int i = 0;
- while(*(string + i) != '\0')
- {
- if (*(string + i) == oldC)
- {
- *(string + i) = newC;
- }
- i++;
- }
- use arrow -> to allocate structure pointer to value without dereferencing.
- e.g. *(string + 1) = pointer[0].pointer;
- now is (string + 1)->pointer[0].pointer;
- char ** B (A double pointer; a pointer address pointing to another pointer address.);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement