Question

In: Computer Science

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 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

I had updated the answer. Please check the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 10000
void extract_phone(char *input, char *phone)
{
    char *temp;
    strcpy(phone, "");
    // Split the tring ,when the first "_" was found
    temp = strtok(input, "_");
    // Repeat the split for two times to get the last string
    temp = strtok(NULL, "_");
    temp = strtok(NULL, "_");
    // copy that  string to the pointer phone
    strcpy(phone, temp);
}
int main()
{
    char buffer[MAX_LEN];
    char phone[MAX_LEN];
    char filename[100], outputfilename[100] = "phone_";
    printf("\nEnter the file name : ");
    scanf("%s", filename);
    strcat(outputfilename, filename);
    printf("Output file name : %s\n", outputfilename);

    FILE *fp;
    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("Failed to open the input file: ");
        return 1;
    }
    FILE *fq;
    fq = fopen(outputfilename, "w");
    if (fq == NULL)
    {
        perror("Failed to open the output file: ");
        return 1;
    }

    // -1 to allow room for NULL terminator for really long string
    while (fgets(buffer, MAX_LEN - 1, fp))
    {
        // Remove trailing newline
        buffer[strcspn(buffer, "\n")] = 0;

        extract_phone(buffer, phone);
        // Write the returned value to the file
        fprintf(fq, "%s\n", phone);
    }

    fclose(fp);
    fclose(fq);
    return 0;
}

Here the file will be created with only the phone numbers. And the file name will be as specified in the question, ("ie. phone_input_names.txt")

As in the question, I had not printed any other details, than specified in the question, To see the file , just open the output file


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." 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...
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....
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)
C++ language. Suppose you are given a list of students registered in a course and you...
C++ language. Suppose you are given a list of students registered in a course and you are required to implement an Linked List of student. Each student in the list has name, regNo and cGpa. You task is to implement following function related to Linked List. insertStudent() : This function, when called will prompt user to enter his/her choice of insertion. The options are 1. Insert student at the start of linked list 2. Insert student at the end of...
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.
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books...
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT