-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexTerminateHandlerCloseFile.cpp
More file actions
53 lines (47 loc) · 1.48 KB
/
exTerminateHandlerCloseFile.cpp
File metadata and controls
53 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//A short example that shows how to close files and do exceptions handling by defining
//a custom terminate handler that's passed to std::set_terminate()
//Usually files are closed anyway
//So consider this just as an example of exceptions handling
//std::flush is also called to make sure the error message is output to console;
#include <cstdio>
#include <exception>
#include <fstream>
#include <iostream>
using namespace std;
std::ofstream outpFile;
//A custom terminate Handler that closes a files before doing other things
void terminateHandler(){
outpFile.close();
int res=remove("asciiList.txt");
std::cout << "User requested to write zero A's to file thus the file is closed, removed and the program is aborted" << std::flush;
std::abort();
}
int main(){
//check if file exits
std::ifstream strFileCheck("asciiList.txt");
if(strFileCheck){
//if file exists delete file
int res = remove("asciiList.txt");
if(res!=0){
std::cout << "Error deleting file!";
exit(0);
}
}
else{
outpFile.open("asciiList.txt", std::ofstream::out | std::ofstream::app);
}
int numA, i;
std::cout << "enter numer of the ascii charcter 'a' to be written to the file:\n";
cin >> numA;
if(numA<=0)
terminateHandler();
if(numA==1){
outpFile << "a";
outpFile.close();
}
if(numA>=1){
for(i=0; i<numA; i++)
outpFile << "a";
outpFile.close();
}
}