abort() function in C++

In this tutorial, we are going to learn the abort() function using C++.

Syntax of abort function in C++

void abort (void);

abort() function will not close the file that is open. It keeps temporary files safer without deleting them and may not flush stream buffer. Also, those functions registered with atexit() are not called.

The abort function is included in stdlib.h header file. This function raises SIGABRT signat to terminate the process.

See code below it may not write, “codespeedy.”

#include<stdio.h> 
#include<stdlib.h> 
int main() 
{ 
  FILE *fp = fopen("C:\\myfile.txt", "w"); 
    
  if(fp == NULL) 
  { 
    printf("\n could not open file "); 
    getchar(); 
    exit(1); 
  }   
    
  fprintf(fp, "%s", "Codespeedy"); 
    
  
  abort(); 
    
  getchar(); 
  return 0;   
}
output:

could not open file

 

You can also read: Difference between exit() and _Exit() in C++

atexit() function in C++

Leave a Reply

Your email address will not be published. Required fields are marked *