Question
Make a program that can write advice to a file. Your program should not require a data file to be present when it is run.
Make a program that can write 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 2011 students, managed to come up with code that worked with both MSVC++ and DevC++.
In MSVC++ his code:
inStream.open("advice.txt");
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.open("advice.txt", 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 3 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.open("advice.txt",ios::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.
Example Output:
First Run (Dev C++):
Could not open Advice File.
Assumption: first run - creating a new file...
Enter your phrase for the next user:
Never take advice from a programmer.
Press any key to continue . . .
Second Run(Dev C++)
Found Advice File.
Old 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 . . .
I do have code but there are some errors that I can't fix. Here is the code:
#include ERRORS I GET: cannot open source file "bits/stdc++.h" identifier "getline" is undefined identifier "getline" is undefined Cannot open include file: 'bits/stdc++.h': No such file or directory
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