Question

In: Computer Science

Suppose you are given a file containing a list of names and phone numbers in the...

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.

Solutions

Expert Solution

//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 :)


Related Solutions

Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C 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...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." 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...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language 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....
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." 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...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
Suppose a file (whose name is given through an argument by the user) contains student names...
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to...
Suppose a file (whose name is given through an argument by the user) contains student names...
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT