Question
C++ in Linux Description : You will implement reading & writing to files and passing by reference . Problem: You will be given a list
C++
in Linux
Description: You will implement reading & writing to files and passing by reference.
Problem: You will be given a list of words in a file. You must then replace all words that start with any of
the characters in a list (from command line args). Finally, you will output the changed list to a file.
You will be reading your arguments from the command line. The name of the input file will be passed into your program as argument 1, argv[1]. The name of the output file will be passed into your program as argument 2, argv[2] . The number of words will be passed into your program as argument 3, argv[3]. The letters to replace will be passed into your program as argument 4, argv[4] .
You must create an array called words, then you must read the contents of this file into it. You must use the function:
void readFile(string fileName, string words[])
Then for every character in the list of characters, replace all words that start with the that character. These words will be replaced with four dashes, ----.
You must use the function:
void replaceWord(string &word, char toReplaceInWord)
Then you must write the array words to file in. You must use the function:
void writeFile(string fileName, string words[], int numberOfWords)
Here is the main function that you must use. You may not add or remove parameters or change the names of the variables. You must initialize the values of the variables.:
int main(int argc, char* argv[]) {
//Check correct number of arguments if( argc != 5 ){
cout<<"Error" << endl;
return 0; }
//Handling cmd line args //YOU MUST INITIALIZE THEIR VALUES! string inputFileName; string outputFileName; int numberOfWords; char* lettersToReplace;
/* * Initialize your variables here */
//Checking valid input if( numberOfWords <= 0 ){
cout << "Error" << endl;
return 0; }
string words[numberOfWords]; //Reading input from file readFile(inputFileName, words, numberOfWords);
/* * Your code goes here.... */
//Output the file writeFile(outputFileName, words, numberOfWords); return 0;
}
Sample Input:
./hw3.out words.txt replacedWords.txt 20 JCBER
words.txt:
Java R Bash Lisp Unix Shell TeX Index out of Bounds
Quality Fortran Go
Stack Overflow Objective-C Assembly Visual Basic C++
Haskell Python Matlab JavaScript Erlang Ruby Groovy Tickle Clojure Scala Rust Mathematica
Sample Output: replacedWords.txt
---- ---- ---- Lisp Unix Shell TeX
Index out of Bounds Quality Fortran Go
Stack Overflow Objective-C Assembly Visual Basic ----
Haskell Python Matlab ---- ---- ---- Groovy Tickle ---- Scala ---- Mathematica
Everyones output MUST match this exactly. Every character. This means you. This is automatically graded by a computer program.
Note/ Hint:
- You must use command line arguments for this assignment.
- You must check that there are the correct number of arguments passed in.
- You must check that the numberOfWords > 0
- You should use getline(...) to read the file. Research how this function works.
http://www.cplusplus.com/reference/string/string/getline/
- USE GOOGLE! (Seriously)
- USE StackOverflow
- Do not use code that you dont fully understand. If there is an error with it, youll never figure it out.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started