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

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

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

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

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line 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 new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers 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
ADVERTISEMENT