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...
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. Both arguments are to be numerical values restricted to 22-bit unsigned integers. The input must be fully qualified, and the user should be notified of any errors with the input provided by the user. 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...
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...
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH Hint: Use the fgets function to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT