Question

Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

Can someone help me put the string removee the return to an outfile and fix??

prompt:

CS 575 LabEx14: no e’s

Let the user specify an input file and an output file (text files).

Read the input file and write the output file; the output file should be the same as the

input file, except that it has no e’s.

For example, if the input file had the line.

Streets meet in the deep.

Then the output file would have the corresponding line

Strts mt in th dp.

#include <iostream> // ifstream ofstream

#include <fstream> // cin cout

#include <sstream> // istringstream ostringstream

#include <string> // string

#include <cstdlib> // exit

using namespace std;

bool die(const string & msg);

string getString(const string & prompt = "Type a line for me: ");

bool open(ifstream fin, const string & fileName);

bool open(ofstream fout, const string & fileName);

string removee(const string & s);

int main() {

fstream fin, fout;

  

string inName(getString("Name of input file: "));

fin.open(inName);

if (!fin) die("Can't Open " + inName + " For Input.");

  

string outName(getString("Name of output file: "));

fout.open(outName);

if (!fout) die("Can't Open " + outName + " For Input.");

  

  

fout.close();

if(!fout) die("Problem writing to " + outName);

fin.close();

}

bool die(const string & msg){

cout << "Fatal error: " << msg << endl;

exit(EXIT_FAILURE);

}

string getString(const string & prompt){

cout << prompt;

string ret;

getline(cin, ret);

return ret;

}

bool open(ifstream & fin, const string & fileName){

fin.open(fileName);

return !!fin;

}

bool open(ofstream & fout, const string & fileName){

ifstream tin (fileName);

if (tin){

return false; // tin's life ends, file closed.

}

fout.open(fileName);

return !!fout;

}

string removee(const string & s){

string ret;

for (unsigned pos = 0; pos < s.size(); pos++) {

if (s[pos] != 'e')

ret += s[pos];

}

return ret;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream> // ifstream ofstream
#include <fstream> // cin cout
#include <sstream> // istringstream ostringstream
#include <string> // string
#include <cstdlib> // exit
using namespace std;

// Prototype of functions
bool die(const string & msg);
string getString(const string & prompt = "Type a line for me: ");
bool open(ifstream fin, const string & fileName);
bool open(ofstream fout, const string & fileName);
string removee(const string & s);
string readString(ifstream &);
void writeString(ofstream&, string);

// main function definition
int main()
{
// ifstream object declared to read file
ifstream fin;
// ofstream object declared to read file
ofstream fout;

// Calls the function to accept the file name to read
string inName = getString("Name of input file: ");

// Opens the file for reading
fin.open(inName.c_str());

// Checks if file is unable to open display error message
if (!fin)
die("Can't Open " + inName + " For Input.");

// Calls the function to read the data from file and store the return string
string original = readString(fin);

// Calls the function to accept the file name to write
string outName = getString("Name of output file: ");

// Opens the file for writing
fout.open(outName.c_str());

// Checks if file is unable to open display error message
if (!fout)
die("Can't Open " + outName + " For Input.");
// close the file
fout.close();

// Checks if file is unable to open for writing display error message
if(!fout)
die("Problem writing to " + outName);
// close the file
fin.close();

// Calls the function to remove e's from the string
string removedStr = removee(original);
// Opens the file for writing
fout.open(outName.c_str());
// Calls the function to write the removed e's string
writeString(fout, removedStr);
}// End of main function

// Function to display error message and exit the program
bool die(const string & msg)
{
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}// End of function

// Function to display message, accept a file name and returns it
string getString(const string & prompt)
{
// Displays message
cout << prompt;
string ret;
// Accepts the file name
getline(cin, ret);
// Returns the file name
return ret;
}// End of function

// Function to check file can be open for reading or not
bool open(ifstream &fin, const string & fileName)
{
// Opens the file for reading
fin.open(fileName.c_str());
return !!fin;
}// End of function

// Opens the file for writing
bool open(ofstream &fout, const string & fileName)
{
// Opens the file in read mode
ifstream tin (fileName.c_str());
if (tin)
return false; // tin's life ends, file closed.

// Opens the file in write mode
fout.open(fileName.c_str());
return !!fout;
}// End of function

// Function to remove the e's from parameter string and return the removed e's string
string removee(const string &s)
{
string ret;
// Loops till end of the string
for (unsigned pos = 0; pos < s.size(); pos++)
{
// Checks if current character is not 'e'
if (s[pos] != 'e')
// Concatenate the current character to ret string
ret += s[pos];
}// End of for loop

// Returns the result string
return ret;
}// End of function

// Function to write string to file
void writeString(ofstream &fWrite, string str)
{
// Displays the removed e's string
cout<<"\n Writes: "<<str;
// Writes the removed e's string
fWrite<<str;
// Close the file
fWrite.close();
}// End of function

// Function to read the string from file and returns it
string readString(ifstream &fRead)
{
string str = "";
string temp;
// Loops till end of the file
while(!fRead.eof())
{
// Reads a word
fRead>>temp;
// Concatenates the word with space to str
str += temp + " ";
}// End of while loop

// Displays the read string
cout<<"\n Read: "<<str<<endl;
// Close the file
fRead.close();
// Returns the string
return str;
}// End of function

Sample Output:

Name of input file: removeE.txt

Read: Streets meet in the deep.
Name of output file: removedEs.txt

Writes: Strts mt in th dp.

removeE.txt file contents

Streets meet in the deep.

removedEs.txt file contents

Strts mt in th dp.

Add a comment
Know the answer?
Add Answer to:
Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...
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
  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

    /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...

  • C++ Can someone please help me with this problem- commenting each line of code so I...

    C++ Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language? I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read. #include<fstream> #include<iostream> using namespace std; int main()...

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

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