Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. //qmoffat 15331488
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <strings.h>
  5. #include <stdbool.h>
  6. FILE *infile;
  7. char filename[50], line[120], sterm[60], name[80], fname[40], lname[40], phone[20];
  8. char temp;
  9. bool printed;
  10.  
  11. void seperate(char deli, char *s, char *v1, char *v2);
  12. void search(char *fnm, char *lnm);
  13.  
  14. int main(){
  15.     printed = false; //bool setup to stop while loop
  16.    
  17.     printf("Enter the filename:");
  18.     gets(filename);
  19.  
  20.     infile = fopen(filename, "r");
  21.    
  22.     if (infile == NULL) {
  23.         printf("ERROR: cannot open %s\n", filename);
  24.         exit(27);
  25.     }
  26.  
  27.     printf("Enter the full name of the person whose number is required:"); //Enter name to be searched for later
  28.     gets(name);
  29.    
  30.     printf("The number for %s:", name);
  31.     while ((fgets(line, 120, infile) != NULL)&& (printed == false)){
  32.         seperate(';', line, name, phone);
  33.         seperate(',', name, lname, fname);
  34.         search(lname,fname); // Search for name entered earlier
  35.     }
  36.     if(printed != true){ //Displays if Name is not found
  37.         printf("No results for %s were found",name);
  38.     }
  39.     fclose(infile);
  40.    
  41. }
  42. void seperate(char deli, char *s, char *v1, char *v2){
  43.     // 3 String Parameters, One Character
  44.     int i; i=0;
  45.    
  46.     // While not at Delimiter, keep loading substring.
  47.     while(line[i] != deli){
  48.         v1[i] = s[i];
  49.         i++;
  50.     }
  51.     // @End of Substring, Move i past the delimiter
  52.     i=i+1;
  53.     // If reusing the index variable, reset it to 0. (while not at 0){ .. }
  54.     while(line[i] != '\n' || '\0'){
  55.         s[i] = v2[i];
  56.         i++;
  57.     }
  58.    
  59.     if(line[i] == '\n'){
  60.         return;
  61.     }
  62.    
  63. }
  64. void search(char *lnm, char *fnm){
  65.     if(strcmp((strcpy(fnm,lnm)),name) == true){
  66.     //Searches for the telephone number for a name supplied by the user
  67.         printf("%s",phone);
  68.         printed = true;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement