Question: Chapter 1 2 Make a program that can write and read advice to a file. Your program should not require a data file to be
Chapter Make a program that can write and read advice to a file. Your program should not require a data file to be present when it is run. If a file needs to be created, your program should do so According to ANSI standards, all compilers should operate the same way. Naturally they do not. David Busch one of my Spring students, managed to come up with code that worked with both MSVC and DevC In MSVC his code: inStream.openadvicetxt; When no file is present, passes the test: Does this file exist? if inStream.fail file exists... Because MS has decided to create a file if none exists. I believe this is not ANSI standard but is more convenient for a programmer. For DevC his code: if inStream.fail file exists here is code to read in the orginal advice. Of course in MSVC the file is empty. else This file does NOT exist. inStream.close; close the input file stream inOutStream.openadvicetxt ios::in ios::out ios::trunc; create new file Handles the case where no file exists and one must be created. I suggest using this approach since it is compiler independent. Important: Note that there are types of file streams, an ofstream only does output, an ifstream only does input. If you want input and output on the same file you use an fstream. inStream.openadvicetxtios::app; will always cause an error if you declared ifstream inStream; Unfortunately, it will compile but will always fail at run time because you are trying to append to an input stream. If a file has been opened for writing it must be flushed and closed before opening it for reading. Example Output: Read or writerwwCould not open Advice File.Assumption: first run creating a new file...Enter your phrase for the next user:Never take advice from a programmer.Read or writerwrOld Advice:Never take advice from a programmer.Enter your phrase for the next user:Ok I never will! Ooops, I just did.Press any key to contiNue. In c
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
