Question
Consider the following C++ program. It has a for loop that prints a common English pangram (a phrase that uses all of the letters in
Consider the following C++ program. It has a for loop that prints a common English pangram (a phrase that uses all of the letters in the alphabet) 10 times.
#include#include #include using namespace std; int main() { // Loop printing phrase string phrase = "A quick brown fox jumps over the lazy dog."; for (int i = 0; i < 10; i++) cout << phrase << endl; return 0; }
Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages. Run your program and you should see a page full of text. For those of you who took a typing class, this should look painfully familiar.
Step 2: Run your program again using the command "./a.out > words.txt". This time you should not see anything on the screen. This is because Linux uses the ">" for output file redirection. Whenever we type "program_name > file_name" the output of the program will be saved in the specified file. If you use nano to open the file "words.txt" you will see 10 copies of the output phrase.
Step 3: Your next task is to modify the program to write the text to words.txt directly. First, edit your program and add "#include
ofstream outfile; outfile.open("words.txt");
The first line declares "outfile" as an output file variable. The second line creates an empty text file called "words.txt" and connects this file to the "outfile" variable.
Step 4: Now edit your code and change "cout << ..." to "outfile << ...". This will write text to your output file instead of the monitor. You need to make one final change to your program to close your output file after you have finished writing it. Add the line "outfile.close();" just before the "return 0;" line.
Step 5: Delete the file "words.txt" and then compile and run your new program. You do not need to use file redirection this time because your program knows the name of the output file. If you use nano to open the file "words.txt" you will see 10 copies of the output phrase. Congratulations, you have created an output text file.
Step 6: Sometimes a program is not able to create an output file, either because the directory permissions do not allow it, or because the directory does not exist. Edit your program and change your output file from "words.txt" to "silly/words.txt". Now delete the file "words.txt" and run your program. You should see that no output file was created because the directory does not exist. Sadly, your program did not tell the user that anything was wrong.
Step 7: The C++ function "outfile.fail()" will return true if the file open failed for some reason. Edit your program again and insert the following lines after your outfile.open() code. The exit function in the
if (outfile.fail()) { cout << "Error: could not open output file "; exit (EXIT_FAILURE); }
Step 8: Compile and run your program again to verify that the error above is printed when you try to open the output file "silly/words.txt". Once you are sure this error checking is working, change the output file name back to "words.txt".
Step 9: Your final task is to read the "words.txt" file one word at a time and print these words on the screen. Edit your program and add the following code just before the "return 0;" line. You will need to replace the two comments with the appropriate
// Open "words.txt" file here string word; while (infile >> word) cout << word << " "; cout << endl; // Close "words.txt" file here
Step 10: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.
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