When you open a file, all kinds of things can go wrong. A file lives on a physical device — a fixed disk, for example, or perhaps on a flash drive or SD card — and you can run into problems when working with physical devices.
For example, part of the disk might be damaged, causing an existing file to become corrupted. Or, less disastrous, you might run out of disk space. Or, even less disastrous, you might try to open a file in a directory that doesn’t exist.
Feb 29, 2016 Purpose: The purpose of this program is the following: Read in the input file and organize the data inside from lowest to highest. Send the new data into an output text file. Close everything and reopen the output text file onto the screen. How can I accomplish the above with the Dev C compiler? The code below is error-free, as far as I can tell. Mar 06, 2018 The instructions here are as per version 4.9.9.2 of Dev C. I don’t think there will be much changes in any older version. So here we go: 1. Go to the Tools menu. In the Tools menu, you should find and option called Compiler Options.
If you try to open a file for writing by specifying a full path and filename but the directory does not exist, the computer responds differently, depending on the operating system you’re using. If you’re unsure how your particular operating system will respond, try writing a simple test application that tries to create and open something like /abc/def/ghi/jkl/abc.txt. (Of course, you’ll want to be sure to use a directory that doesn’t exist.)
Then one of two things will happen: Either the directory and the file will get created, or nothing will happen.
For example, on a Windows system, if we attempt to create a file in a directory that doesn’t exist, the system does not create the directory. That’s because deep down inside, the application ultimately calls an operating system function that does the dirty work of creating the file. And this particular operating system function (it’s called CreateFile(), if you even care) has a rule that it will not create a directory for you.
If you want to determine whether the ostream class was unable to create a file, you can call its fail() member function. This function returns true if the object couldn’t create the file. And that’s what happens when a directory doesn’t exist. The DirectoryCheck01 example shown demonstrates an example of this.
When you run this code, assuming that you don’t have a directory called /abc/def/ghi on your system, you should see the message Couldn’t open the file! Assuming that your particular operating system doesn’t create a directory in this case; if it does, your computer will open the file, write Hi to it, and move on with its happy life after closing things out.
As an alternative to calling the fail() member function, you can use an operator available in various stream classes. This is !, fondly referred to as the “bang” operator, and you would use it in place of calling fail(), as in this code:
Most people prefer to use !outfile instead of outfile.fail(), although !outfile makes confusing code. The reason is that outfile is an object, and the notion of !outfile simply doesn’t make sense.
In fact, !outfile trips up many beginning programmers. They know that outfile is not a pointer in this sample code, and they wonder how you could test it against 0 as you normally can only do with a pointer. (Remember, by saying !x, where x is some pointer, you’re testing x against 0.) And that simply doesn’t make sense! And so, to avoid confusion, just call fail(). It makes more sense.
Here are some reasons your file creation may choke:
- The directory doesn’t exist.
- You’re out of disk space and out of luck.
- Your application doesn’t have the right permissions to create a file.
- The filename was invalid — that is, it contained characters the operating system doesn’t allow in a filename, such as * or ?.
Like any good application, your application should do two things:
- 1.Check whether a file creation succeeded.
- 2.If the file creation failed, handle it appropriately.Don’t just print a horrible message like Oops!Aborting!, leaving your poor users with no choice but to toss the monitor onto the floor. Instead, do something friendlier — such as presenting a message telling them there’s a problem and suggesting that they might free more disk space.
In every program, there is some data which is taken as input and generate the processed data as output following the input > process > output cycle. Therefore it is essential to know how to provide data as input and how to present the output in the desired form. C++ supports a rich set of I/O functions and operations to do this. As these functions use the advanced features of C++, programmers need to know a lot about them before implementing them in any program. It is to be noted that C++ supports all of C's rich sets of I/O functions. Programmers can use any of them within a C++ program. In this chapter, you will learn about how to manage the input/output capabilities of a C++ program.
The input/output in C++
C does not have built-in input/output facilities. Instead, it left the I/O to the compiler as external library functions (such as printf and scanf) in stdio (standard input-output) library. The ANSI C standard formalized these IO functions into Standard IO package (stdio.h). Likewise, C++ continues this approach and formalizes IO in iostream and fstream libraries.
Features of I/O in C++
- C++ IO is type safe.
- C++ IO operations are based on streams of bytes and are device independent.
Streams in C++
C++ IO is based on streams, which are a sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). I/O systems in C++ are designed to work with a wide variety of devices including terminals, disks and tape drives. The input-output system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as a stream. A stream is a sequence of bytes which acts either as a source from which input data can be obtained or as a destination to which output data can be sent. The source stream which provides data to the program is called the input stream and the destination stream which receives output from the program is called the output stream.
Dev C++ Read Text File
Follow the steps below to perform input and output:
- Construct a stream object.
- Connect (Associate) the stream object to an actual IO device
- Perform input/output operations on the stream, via the functions defined in the stream's public interface in a device-independent manner.
- Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
- Free the stream object.
Unformatted input/output operations
You have already used the cin and cout (pre-defined in the iostream file) for input and output of data of various types. This has been made possible by overloading the operators << and >> to recognize all the basic C++ types.
- cin standard input stream
- cout standard output stream
C++ Input And Output Files
put() and get() functions
The classes istream and ostream defines two member functions get() and put() respectively to handle single character input/output operations.
Get() function is of two types:
- get(char *)
- get(void)
Both of them can be used to fetch a character including blank space, tab or new-line character.
Code snippet
Similarly, the function put(), a member of a stream class can be used to output a line of text character by character.
Example:
getline() and write()
You can read and display lines of text more efficiently using the lie oriented input/output functions. They are:
- getline()
- write()
The getline() function reads the entire line of texts that ends with a newline character. The general form of getline() is:
The write() function displays the entire line of text, and the general form of writing this function is: