Write a program that takes two command line arguments: an input file and an outp
ID: 3572760 • Letter: W
Question
Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message.
Notes:
This program can be written in a single main method
Remember that Scanner objects can be opened on the keyboard, a file, or a String.
You may assume that the file ends with a newline sequence (i.e., the file ends with ).
A sample text file is attached.
-------------------------------------------------------------------------------------------------------------------------------------------
Sample text file:
Explanation / Answer
#include <stdio.h>
int main ( int argc, char *argv[] )
{
char filename[100];
char x;
char *p;
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
// We assume argv[1] is a filename to open
FILE *file = fopen( argv[1], "r" );
/* fopen returns 0, the NULL pointer, on failure */
if ( file == 0 )
{
printf( "Could not open file " );
}
if ( file == 0 )
{
printf( "Could not open file " );
}
while ( ( x = fgetc( file ) ) != EOF )
{
p=&x;
if (p== ' ')
{
p=p-1;
*p='*';
}
}
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
fclose( file );
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.