In: Computer Science
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone."
In C, Write a program to extract the phone numbers and store them in the output file.
Example input/output:
Enter the file name: input_names.txt
Output file name: phone_input_names.txt
1) Name your program phone_numbers.c
2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length of each line in the input file is no more than 10000 characters.
3) The program should include the following function: void extract_phone(char *input, char *phone); The function expects input to point to a string containing a line in the “First_Last_Phone” form. In the function, you will find and store the phone number in string phone.
//C CODE TO COPY//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<queue>
struct phones
{
char phone[100];
phones *next;
};
phones *head;
//function decleration
void extract_phone(char *input, char *phone);
void insert_phone(char p[100]);
void write_phone(char *input);
void extract_phone(char *input, char *phone)
{
FILE* filePointer;
int bufferLength = 255;
char *pt = "";
filePointer = fopen(input, "r");
if (filePointer == NULL)
{
printf("\nfile not found %s",
input);
// Program exits if file pointer
returns NULL.
exit(1);
}
int counter = 0;
char *line = new char[1000];
while (fgets(line, bufferLength, filePointer)) {
pt = new char[100];
pt = strtok(line, "_");
counter = 0;
while (pt != NULL){
if (counter ==
2)
insert_phone(pt);
pt =
strtok(NULL, "_");
counter++;
}
char *line = new char[10000];
}
free(pt);
free(line);
fclose(filePointer);
write_phone(input);
}
void insert_phone(char p[100])
{
phones *ph = new phones();
strcpy(ph->phone, p);
ph->next = NULL;
if (head == NULL)
head = ph;
else
{
phones *temp = head;
while (temp->next != NULL)
temp =
temp->next;
temp->next = ph;
}
}
void write_phone(char *input)
{
FILE *fp;
char output_file[1000] = "phone_";
strcat(output_file, input);
fp = fopen(output_file, "w");
if (fp == NULL)
{
printf("\nfile not found %s",
input);
// Program exits if file pointer
returns NULL.
exit(1);
}
phones *temp = head;
while (temp!=NULL){
fprintf(fp, "%s\n",
temp->phone);
temp = temp->next;
}
fclose(fp);
printf("\nphones data written to %s", input);
}
//main driver function
int main(void)
{
head = NULL;
char file_name[100];
printf("\nEnter input file name: ");
scanf("%s", file_name);
extract_phone(file_name, "");
printf("\n\n");
return 0;
}
//OUTPUT//
Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the
answer :)