In: Computer Science
Write a simple phonebook using c++ that read text file name list.txt and has: first name, last name, and phone number as the example:
MIKEL BUETTNER 3545044
ENOCH BUGG 2856220
BRENDON LAVALLEY 433312
QUINTIN CREEK 5200413
JAMISON MILLETT 6243458
FLORENCIO PUMPHREY 3296862
DARRICK FREGOSO 6868442
TOBIAS GLASSMAN 6040564
and then ask the user to add a new contact first name, last name, and phone number and same the information into a file.
Use array and pointer
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//creating the struct for the local storage of the phonebook for
firstname , lastname and phonenumber as array of char
typedef struct phonebook_detail
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
//Declaring the FILE pointer *pRead to read and *pWrite pointer to write the file
FILE *pRead;
FILE *pWrite;
//Function to Add_new_Entry
void Add_new_Entry (phone * phonebook)
{
//use the pWrite pointer to open the list.txt file using fopen
pWrite = fopen("list.txt", "a");
//Take the use
input as First name, Last name and phone number
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number: ");
scanf("%s", phonebook[counter-1].PhoneNumber);
// Adding the details took by user as input to the file using fprint and pWrite
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
// close the file after saving the detail's in the file
fclose(pWrite);
}
int main(){
//Declaring the phonebook pointer of typedef phone which used to store the user input
phone *phonebook;
// Allocate the memory using malloc function of typedef phone
phonebook = (phone*) malloc(sizeof(phone)*100);
// call the function adding new entry
Add_new_Entry (phonebook);
}