Thread: Phone book C program - command line arguments

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    7

    Phone book C program - command line arguments

    I have a following code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
     
    #define MAX_NAME (128)
    #define MAX_NUM (64)
    #define MAX (128)
    typedef struct Contact {
        char name[MAX_NAME];
        char number[MAX_NUM];
    } Contact;
     
    char matchTable[10][9] = {
        "0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
        "5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
    };
     
     
    bool find(char c, char key){
     
        int j = key - '0';
        for (int i = 0; matchTable[j][i] != '\0'; i++){
            if (c == matchTable[j][i])
                return true;
        }
        return false;
     
    }
     
    bool matches(char* src, char* key){
     
        unsigned int i,j;
     
        for (i = 0; src[i] != '\0'; i++){
            int tmp = i;
            for (j = 0; key[j] != '\0'; j++){
                if (find(src[tmp], key[j]))
                    tmp++;
                else
                    break;
            }
            if (j == strlen(key))
                return true;
        }
        return false;
    }
     
    int main(int argc, char** argv){
     
        char key[MAX_NUM];
        scanf("%s", key);
     
        Contact contacts[MAX];
     
        int k = 0;
     
        FILE* f = fopen("1.txt", "r");
        char nameBuf[MAX_NAME];
        char numBuf[MAX_NUM];
       
        while (fgets(nameBuf, MAX_NAME, f)
                && fgets(numBuf, MAX_NUM, f)){
            strcpy(contacts[k].name, nameBuf);
            strcpy(contacts[k].number, numBuf);      
            k++;
        }
     
        for (int i = 0; i < k; i++){
            bool matchesName = matches(contacts[i].name, key);
            bool matchesNumber = matches(contacts[i].number, key);
           
            if (matchesName || matchesNumber)
                printf("%s\n", contacts[i].name);
        }
     
        fclose(f);
       
        return 0;
    }
    It's a phone book program, searching contacts from txt folder by putting number in command line.
    Output- all contacts that suits following number .
    But I can't use FILE * , fopen fclose, so no file funtions are allowed.
    Input in terminal should be:
    ./project1 <contacts.txt

    Searching example:
    ./project1 509 <contacts.txt
    John Smith, 509012769
    James Murphy, 509930711

    So basically, I need to replace file functionsfunctions, read names and numbers from txt folder, probably with fgets funtion it should work.

    Any good advise is welcomed. If you need further information or problem with understaning source, leave a message.
    Last edited by VasilP; 11-17-2019 at 07:17 AM. Reason: Input adaptation

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show your input file "contacts.txt".

    Do you realize that your run line ("./project1 <contacts.txt") is using input redirection? Which means that the above file should contain all your input in the order required.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    7

    Input line

    Input txt has following structure:
    name
    number
    name
    number
    ...
    ...
    I managed to read it line by line, determine what is number and what name of a single person in structure. It works with fgets function, but I have problem reading it together with other input (number that I type on command line for searching, in source code variable "key").

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but I have problem reading it together with other input (number that I type on command line for searching, in source code variable "key").
    All input should be coming from that input file.

    Input txt has following structure:
    That structure seems wrong. What about the first entry?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2014, 01:21 PM
  2. command-line arguments or parameters to a program
    By c_lady in forum C Programming
    Replies: 2
    Last Post: 06-21-2010, 10:28 AM
  3. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  4. command line arguments
    By Mr.Pink in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 05:32 PM
  5. Replies: 5
    Last Post: 06-24-2003, 01:13 AM

Tags for this Thread