Error Checking ofstream?

#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include"person.h"
using weightGain::person;
using std::getline;
using std::cerr;
using std::string;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::istream;
using std::cout;

string get_string(istream& s){
char a[50];
s.get(a, 49, ',');
if(s.peek()== EOF){
cout << "End of file Reached\n";
exit(1);
}
if(s.peek()!= ','){
cout << "Comma was missing before the end of file was reached\nExiting Program\n";
exit(1);
}
s.ignore();
return a;
};

int main(){

string first;
string last;
int weight;

ifstream inputPerson("person.dat");
if(!inputPerson){
cerr << "Cannot locate file needed to be opened";
exit(1);
}
if(inputPerson.fail()){
cerr << "\nFile failed.\n";
}
ofstream outputPerson("output.dat");

for(;;){
last = get_string(inputPerson);
first = get_string(inputPerson);

inputPerson >> weight;

person newPerson(last, first, weight);
newPerson.weightGain();

outputPerson << newPerson;

}
return 0;
}

Here is my code I am just wondering is it necessary to error check the ofsream file? If it is then is there a way to do it at the same times as the input file or should I just run the checks twice. Also is there any other checks anyone would suggest?
Last edited on
The other error checking functions are used while reading the file. If !file is false then that's it. You can safely start reading the file.
Also, bad() doesn't check that the file is "bad" (whatever you thought that means). It checks all error conditions and returns true if any of them is true.
Last edited on
Okay I took it out for now. How should I use .bad() properly? I looked it up in the documentation on this site but it does not give a real good explanation
Topic archived. No new replies allowed.