Teachers Instructions In each progject, you should open the files using any path
ID: 3887428 • Letter: T
Question
Teachers Instructions
In each progject, you should open the files using any paths to folders but remember that on a Windows system, you must use the '' character when specifying directories. If you copy the input file to the same directory as your project executible, you can simply open statements using the following:
cin >> filename; // where the user enters "hw4pr1input.txt"
inputFileStream.open(filename);
That means the input and output files need to be in the same folder as your compiled and linked executable code (your EXE file). That will vary by compiler, but it is not difficult to place your files in the correct folder. I don't want to see paths to folders on your computer, those paths will not work on mine when I go to grade your homework. If I have to modify your open statements or if I have to answer prompts for file names I will count off.
Your program should first display the current text of the file provided by the assignment. It should then prompt the user to enter new text. That new text should replace the old in the file, not append to it. The new text may contain more than one entered line, so you will ask the user to enter two end-of-line characters to signal the end of input. To do this properly, you'll need to read the input one character at a time in a way that lets you see every entered character, especially the controlling characters such as ' '. [Hint: using the >> operator to read into a char type value is not sufficient. That operator ignores "white space" like spaces, tabs and ENTERs.] Also, when checking two consecutive input characters, it is far easier to remember the previous character than to anticipate the next. Trying to use a function that let's you look ahead isn't necessary. (Some of you will find the PEEK function with Internet searches and attempt to use it here. That function has its uses, but this isn't a particularly good place for it. It is far simpler just to remember the previous character from iteration to iteration.)
Text Book Question
Write a C++ program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Enter key two times. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character ' '. (File name hw4pr02input.txt)
Explanation / Answer
#include <fstream>
#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
//Function to read data from specified file
void readFile(char fileName[])
{
//Creates an object of ifstream
ifstream f;
//Opens the file for reading
f.open(fileName);
//To store read data from file
char s[100];
//Displays error if file not found
if(!f)
{
cout<<"Error in opening file..!!";
exit(0);
}//End of if
//Loops till end of file
while(!f.eof())
{
//Read data from file and stores it in s
f>>s;
//Display the data
cout<<s<<" ";
}//End of while
cout<<" ";
//Close the file
f.close();
}//End of function
//Write the data to specified file
void writeFile(char fileName[])
{
//Creates an object of ofstream
ofstream fw;
//Opens the file for writing
fw.open(fileName);
//To store the accepted data from console
char s[100];
//Counter for number of lines
int counter = 0;
//Displays error if file not exits
if(!fw)
{
cout<<"Error in opening file..!!";
exit(0);
}//End of if
cout<<" Enter the advice: ";
//Cleans the buffer
fflush(stdin);
//Loops infinite tile
while(1)
{
//Accepts data from the console
cin.getline(s, 80);
//Writes data to file
fw<<s;
//Increase the line counter by one
counter++;
//Checks if the counter is 2 come out of the loop
if(counter == 2)
break;
else
fw<<" ";
}//End of while
cout<<" ";
//Close the file
fw.close();
}//End of function
//Main function definition
int main()
{
//To store file name
char fileName[20];
//Accept file name
cout << "Please enter a file name to read: ";
cin>>fileName;
//Read the file
readFile(fileName);
//Write the file
writeFile(fileName);
}//End of main function
Sample Run:
Please enter a file name to read: advice.txt
Do good get good. Help others.
Enter the advice: Belive in god.
God helps everybody.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.