Question

Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

Objective:

Use input/output files, strings, and command line arguments.

Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file.

For example, if the input file is given as follows:


This is line 1


  
This is line 2

This is line 3   

Correct output:
1. This is line 1
2. This is line 2
3. This is line 3

The file names must be provided at the command line (see "Parsing command line arguments" below). To use your program, a user would type:

     ./a.out first.txt second.txt

Where "first.txt" is the input file name and "second.txt" is the output file name.

Your program must print usage/help message and quit if any of the following occurs:

  • Wrong number of arguments at the command line.

  • The input file fails to open

  • The output file fails to open

  • Your program must include, at least, the following functions:

  • A function to display a usage message to the user.

  • A function that removes all white spaces from the beginning and end of the line. Should

    take a string and return a string.

    Parsing command line arguments

    In C++ you can input data (in the form of strings) into your program on the command line (command line arguments). You can capture these data by adding two parameters to your main program as follows:
    int main (int argc, char *argv[])

    argc: (argument count) number of arguments on the line, including the name of the program

    argv: (argument vector) list of c-style strings that represent all the arguments including the name of the program.

    For example, executing the command:

         ./a.out file1.txt file2.txt
    

    Assigns:

         argc = 3
         argv[0] will be "./a.out"
         argv[1] will be "file1.txt"
         argv[2] will be "file2.txt"
    

    To get the file name into your program you would write the following code:

    string inputFileName = argv[1];
    string outputFileName = argv[2];
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please go threw code,comments and output.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void removeWhightSpace(string file1, string file2) // remove blank line function

{

std::ifstream ifile(file1); // input file

std::ofstream ofile(file2); // output file

if (ifile.is_open()) { // open input file

std::string line; // take line to get file

while (getline(ifile, line)) { // get line from file

if(line == "" || line[0] == ' '|| line[0] == '\n') // check null or space in line

continue;

ofile << line << endl; // print line in output file

}

ifile.close(); // close input file

ofile.close(); // close output file

}

}

void usageMessage()

{

cout << "Usage: ./a.out file1.txt file2.txt" << endl;

}

int main(int argc, char * argv[]) // take argument from command line

{

if(argc != 3) // check argument and print message if wrong

{

usageMessage();

return 0;

}

string inputFileName = argv[1]; // take file from argument

string outFileName = argv[2];

removeWhightSpace(inputFileName, outFileName); // call white space function

}

Add a comment
Know the answer?
Add Answer to:
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • in c prog only please!! Name this program one.c - This program takes two command line...

    in c prog only please!! Name this program one.c - This program takes two command line arguments: 1. an input filename 2. a threshold Your program will create two files: 1. even.txt - contains all integers from the input file that are even and greater than the threshold 2. 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....

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • What is the output of the following code given the program was called using the command-line:...

    What is the output of the following code given the program was called using the command-line: a.out input.txt output.txt int main(int argc, char* argv[]) { cout << argc; return 0; } Select one: a. 1 b. 2 c. 3 d. 6 which asnwer it is

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT