C++ in Linux Description : You will implement reading & writing to files and pas
ID: 3885772 • Letter: C
Question
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:
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:
Then you must write the array words to file in. You must use the function:
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[]) {
return 0; }
return 0; }
}
Sample Input:
words.txt:
Sample Output: replacedWords.txt
Everyone’s 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 don’t fully understand. If there is an error with it, you’ll never figure it out.
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
using namespace std;
void readFile(string fileName, string words[], int numOfWords)
{
ifstream fin;
fin.open(fileName);
for(int i = 0; i < numOfWords; i++)
getline(fin, words[i]);
}
void writeFile(string fileName, string words[], int numOfWords)
{
ofstream fout;
fout.open(fileName);
for(int i = 0; i < numOfWords; i++)
fout << words[i];
}
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
*/
inputFileName = argv[1];
outputFileName = argv[2];
numberOfWords = atoi(argv[3]);
lettersToReplace = argv[4];
//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....
*/
for(int i = 0; i < numberOfWords; i++)
{
int j = 0;
while(lettersToReplace[j] != '')
{
if(words.at(0) == lettersToReplace[j])
{
strcpy(words[i], "---- ");
break;
}
j++;
}
}
//Output the file
writeFile(outputFileName, words, numberOfWords);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.