How To Exit Program In Dev C++
In C++, you open a file, you must first obtain a stream. There are the following three types of streams:
- If you run the program through the command prompt, the window will stay up when the program finished, waiting for another command. For the window to close when finished, you will have to run the program by double clicking the.exe, I believe. It's been years since I've used Windows systems.
- Using this information, write a C program that calculates the nth number in a Fibonacci sequence, where the user enters n in the program interactively. For example, if n = 6, the program should display the value 5.' I have created the program, but I want to give the User an option to either go back to the start of the program, or to quit it.
- In C, there are two statements break; and continue; specifically to alter the normal flow of a program. Sometimes, it is desirable to skip the execution of a loop for a certain test condition or terminate it immediately without checking the condition. For example: You want to loop through data of all aged people except people aged 65.
Nov 05, 2017 For the Love of Physics - Walter Lewin - May 16, 2011 - Duration: 1:01:26. Lectures by Walter Lewin. They will make you ♥ Physics. Recommended for you. The C library function void exit(int status) terminates the calling process immediately. Any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal. On unix like operating systems exit belongs to group of system calls. System calls are special calls which enable user code (your code) to call kernel code. So exit call makes some OS specific clean-up actions before returning control to OS, it terminates the program.
- input
- output
- input/output
Create an Input Stream
To create an input stream, you must declare the stream to be of class ifstream. Here is the syntax:
Create an Output Stream
To create an output stream, you must declare it as class ofstream. Here is an example:
Create both Input/Output Streams
Streams that will be performing both input and output operations must be declared as class fstream. Here is an example:
Opening a File in C++
Once a stream has been created, next step is to associate a file with it. And thereafter the file is available (opened) for processing.
Opening of files can be achieved in the following two ways :
- Using the constructor function of the stream class.
- Using the function open().
The first method is preferred when a single file is used with a stream. However, for managing multiple files with the same stream, the second method is preferred. Let's discuss each of these methods one by one.
Opening File Using Constructors
We know that a constructor of class initializes an object of its class when it (the object) is being created. Same way, the constructors of stream classes (ifstream, ofstream, or fstream) are used to initialize file stream objects with the filenames passed to them. This is carried out as explained here:
To open a file named myfile as an input file (i.e., data will be need from it and no other operation like writing or modifying would take place on the file), we shall create a file stream object of input type i.e., ifstream type. Here is an example:
The above given statement creates an object, fin, of input file stream. The object name is a user-defined name (i.e., any valid identifier name can be given). After creating the ifstream object fin, the file myfile is opened and attached to the input stream, fin. Now, both the data being read from myfile has been channelised through the input stream object.
Now to read from this file, this stream object will be used using the getfrom operator ('>>'). Here is an example:
Similarly, when you want a program to write a file i.e., to open an output file (on which no operation can take place except writing only). This will be accomplish by
- creating ofstream object to manage the output stream
- associating that object with a particular file
Here is an example,
How To Use Dev C++ Compiler
This would create an output stream, object named as fout and attach the file secret with it.
Now, to write something to it, you can use << (put to operator) in familiar way. Here is an example,
The connections with a file are closed automatically when the input and the output stream objects expires i.e., when they go out of scope. (For example, a global object expires when the program terminates). Also, you can close a connection with a file explicitly by using the close() method :
Closing such a connection does not eliminate the stream; it just disconnects it from the file. The stream still remains there. For example, after the above statements, the streams fin and fout still exist along with the buffers they manage. You can reconnect the stream to the same file or to another file, if required. Closing a file flushes the buffer which means the data remaining in the buffer (input or output stream) is moved out of it in the direction it is ought to be. For example, when an input file's connection is closed, the data is moved from the input buffer to the program and when an output file's connection is closed, the data is moved from the output buffer to the disk file.

Opening Files Using Open() Function
There may be situations requiring a program to open more than one file. The strategy for opening multiple files depends upon how they will be used. If the situation requires simultaneous processing of two files, then you need to create a separate stream for each file. However, if the situation demands sequential processing of files (i.e., processing them one by one), then you can open a single stream and associate it with each file in turn. To use this approach, declare a stream object without initializing it, then use a second statement to associate the stream with a file. For example,
The above code lets you handle reading two files in succession. Note that the first file is closed before opening the second one. This is necessary because a stream can be connected to only one file at a time.
The Concept of File Modes
The filemode describes how a file is to be used : to read from it, to write to it, to append it, and so on.
When you associate a stream with a file, either by initializing a file stream object with a file name or by using the open() method, you can provide a second argument specifying the file mode, as mentioned below :
The second method argument of open(), the filemode, is of type int, and you can choose one from several constants defined in the ios class.
List of File Modes in C++
C++ Code To Exit Program
Following table lists the filemodes available in C++ with their meaning :
Constant | Meaning | Stream Type |
---|---|---|
ios :: in | It opens file for reading, i.e., in input mode. | ifstream |
ios :: out | It opens file for writing, i.e., in output mode. This also opens the file in ios :: trunc mode, by default. This means an existing file is truncated when opened, i.e., its previous contents are discarded. | ofstream |
ios :: ate | This seeks to end-of-file upon opening of the file. I/O operations can still occur anywhere within the file. | ofstream ifstream |
ios :: app | This causes all output to that file to be appended to the end. This value can be used only with files capable of output. | ofstream |
ios :: trunc | This value causes the contents of a pre-existing file by the same name to be destroyed and truncates the file to zero length. | ofstream |
ios :: nocreate | This cause the open() function to fail if the file does not already exist. It will not create a new file with that name. | ofstream |
ios :: noreplace | This causes the open() function to fail if the file already exists. This is used when you want to create a new file and at the same time. | ofstream |
ios :: binary | This causes a file to be opened in binary mode. By default, files are opened in text mode. When a file is opened in text mode, various character translations may take place, such as the conversion of carriage-return into newlines. However, no such character translations occur in file opened in binary mode. | ofstream ifstream |
Exit Function In Dev C++
If the ifstream and ofstream constructors and the open() methods take two arguments each, how have we got by using just one in the previous examples ? As you probably have guessed, the prototypes for these class member functions provide default values for the second argument (the filemode argument). For example, the ifstream open() method and constructor use ios :: in (open for reading) as the default value for the mode argument, while the ofstream open() method and constructor use ios :: out (open for writing) as the default.
The fstream class does not provide a mode by default and, therefore, one must specify the mode explicitly when using an object of fstream class.
Both ios::ate and ios::app place you at the end of the file just opened. The difference between the two is that the ios::app mode allows you to add data to the end of the file only, when the ios::ate mode lets you write data anywhere in the file, even over old data.
You can combine two or more filemode constants using the C++ bitwise OR operator (symbol ). For example, the following statement :
will open a file in the append mode if the file exists and will abandon the file opening operation if the file does not exist.
Dev Program Download
To open a binary file, you need to specify ios :: binary along with the file mode, e.g.,
or,
Closing a File in C++
As already mentioned, a file is closed by disconnecting it with the stream it is associated with. The close() function accomplishes this task and it takes the following general form :
For example, if a file Master is connected with an ofstream object fout, its connections with the stream fout can be terminated by the following statement :
C++ Opening and Closing a File Example
Here is an example given, for the complete understanding on:
How To Exit Program In Dev C 2017
- how to open a file in C++ ?
- how to close a file in C++ ?
Dev C++ Program Download
Let's look at this program.
Here is the sample run of the above C++ program:
More Examples
Here are some example programs of C++ listed, that you can go for. These programs uses file: