Question

In: Computer Science

IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...

IN C LANGUAGE

16.15 Lab 5: filter

Name this program filter.c.

  • The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return.
    • fopen() will return 0 if it fails to open a file.
  • Then, the program will read the file string by string (up to 50 characters in length). If the string consists of only uppercase and lowercase characters, print it.
    • You can use isalpha(char) declared in ctype.h to check whether a character is a-z or A-Z.
    • continue and break might be useful.
  • Don't forget to close your files!

Examples

./a.out example.txt output.txt
example.txt output.txt
The Quick Brown Fox Jumps over the Lazy Old Dog. ALABAMA? Roll Tide!!! P2P 1831 (UA) The Quick Brown Fox Jumps over the Lazy Old Roll
./a.out does_not_exist.txt output.txt
Cannot open file 'does_not_exist.txt'

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h> // For exit()
#include <ctype.h>

int main(int argc,char* argv[])
{
   FILE *inputfptr, *ouputfptr;
   char c;

   // Open one file for reading
   inputfptr = fopen(argv[1], "r");
   if (inputfptr == NULL)
   {
       printf("Cannot open file %s \n", argv[1]);
       exit(0);
   }

   // Open another file for writing
   ouputfptr = fopen(argv[2], "w");
   if (ouputfptr == NULL)
   {
       printf("Cannot open file %s \n", argv[2]);
       exit(0);
   }

   // Read contents from file
   c = fgetc(inputfptr);
   int k = 0;
   while (c != EOF)
   {
   char temp[50];
   if(c!=' ')
   {
   temp[k++] = c;
   }
   else
   {
   int t = 1;
   for(int m=0;m<k;m++)
   {
   if(isalpha(temp[m])){}
   else{t=0;break;}
   }
   if(t==1)
   {
   for(int m=0;m<k;m++)
   {
       fputc(temp[m], ouputfptr);
   }
   fputc(' ', ouputfptr);
   }
   k=0;
   }
       c = fgetc(inputfptr);
   }

   printf("\nContents copied to %s", argv[2]);

   fclose(inputfptr);
   fclose(ouputfptr);
   return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

THE SAME PROGRAMME WITHOUT COMMAND LINE ARGUMENTS

#include <stdio.h>
#include <stdlib.h> // For exit()
#include <ctype.h>

int main(int argc,char* argv[])
{
   FILE *inputfptr, *ouputfptr;
   char c;

   // Open one file for reading
   inputfptr = fopen("example.txt", "r");
   if (inputfptr == NULL)
   {
       printf("Cannot open file %s \n", "example.txt");
       exit(0);
   }

   // Open another file for writing
   ouputfptr = fopen("output.txt", "w");
   if (ouputfptr == NULL)
   {
       printf("Cannot open file %s \n", "output.txt");
       exit(0);
   }

   // Read contents from file
   c = fgetc(inputfptr);
   int k = 0;
   while (c != EOF)
   {
   char temp[50];
   if(c!=' ')
   {
   temp[k++] = c;
   }
   else
   {
   int t = 1;
   for(int m=0;m<k;m++)
   {
   if(isalpha(temp[m])){}
   else{t=0;break;}
   }
   if(t==1)
   {
   for(int m=0;m<k;m++)
   {
       fputc(temp[m], ouputfptr);
   }
   fputc(' ', ouputfptr);
   }
   k=0;
   }
       c = fgetc(inputfptr);
   }

   printf("\nContents copied to %s", "output.txt");

   fclose(inputfptr);
   fclose(ouputfptr);
   return 0;
}


Related Solutions

16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the...
16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the program will read...
IN C LANGUAGE 16.16 Lab 5: merge Name this program merge.c - This program will take...
IN C LANGUAGE 16.16 Lab 5: merge Name this program merge.c - This program will take two arguments from the command-line which will be the names of the two text files the program will read from. These text files contain a list of numbers each in ascending order. You'll open the text files and begin merging the two sets of numbers together until every unique number is printed to the screen once and in order. For example: file1.txt file2.txt 1...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT