Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //qmoffat 15331488
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <stdbool.h>
- FILE *infile;
- char filename[50], line[120], sterm[60], name[80], fname[40], lname[40], phone[20];
- char temp;
- bool printed;
- void seperate(char deli, char *s, char *v1, char *v2);
- void search(char *fnm, char *lnm);
- int main(){
- printed = false; //bool setup to stop while loop
- printf("Enter the filename:");
- gets(filename);
- infile = fopen(filename, "r");
- if (infile == NULL) {
- printf("ERROR: cannot open %s\n", filename);
- exit(27);
- }
- printf("Enter the full name of the person whose number is required:"); //Enter name to be searched for later
- gets(name);
- printf("The number for %s:", name);
- while ((fgets(line, 120, infile) != NULL)&& (printed == false)){
- seperate(';', line, name, phone);
- seperate(',', name, lname, fname);
- search(lname,fname); // Search for name entered earlier
- }
- if(printed != true){ //Displays if Name is not found
- printf("No results for %s were found",name);
- }
- fclose(infile);
- }
- void seperate(char deli, char *s, char *v1, char *v2){
- // 3 String Parameters, One Character
- int i; i=0;
- // While not at Delimiter, keep loading substring.
- while(line[i] != deli){
- v1[i] = s[i];
- i++;
- }
- // @End of Substring, Move i past the delimiter
- i=i+1;
- // If reusing the index variable, reset it to 0. (while not at 0){ .. }
- while(line[i] != '\n' || '\0'){
- s[i] = v2[i];
- i++;
- }
- if(line[i] == '\n'){
- return;
- }
- }
- void search(char *lnm, char *fnm){
- if(strcmp((strcpy(fnm,lnm)),name) == true){
- //Searches for the telephone number for a name supplied by the user
- printf("%s",phone);
- printed = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement