File Handling Program In Dev C++

The program prints 'geeksforgeeks' in the file text.txt Description: When an object is created for ofstream class, it allows us to write into a file just like cout. When opening a file with ofstream object if file is present then the content is erased else it is created.

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.

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. 1.Check whether a file creation succeeded.

  2. 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 programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C.

File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.

  • Creation of the new file
  • Opening an existing file
  • Reading from the file
  • Writing to the file
  • Deleting the file

Functions for file handling

There are many functions in the C library to open, read, write, search and close the file. A list of file functions are given below:

No.FunctionDescription
1fopen()opens new or existing file
2fprintf()write data into the file
3fscanf()reads data from the file
4fputc()writes a character into the file
5fgetc()reads a character from file
6fclose()closes the file
7fseek()sets the file pointer to given position
8fputw()writes an integer to file
9fgetw()reads an integer from file
10ftell()returns current position
11rewind()sets the file pointer to the beginning of the file
File handling in c++ examples

Opening File: fopen()

We must open a file before it can be read, write, or update. The fopen() function is used to open a file. The syntax of the fopen() is given below.

File Handling In Java Tutorial

The fopen() function accepts two parameters:

  • The file name (string). If the file is stored at some specific location, then we must mention the path at which the file is stored. For example, a file name can be like 'c://some_folder/some_file.ext'.
  • The mode in which the file is to be opened. It is a string.

File Handling Functions In C

We can use one of the following modes in the fopen() function.

ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode

The fopen function works in the following way.

  • Firstly, It searches the file to be opened.
  • Then, it loads the file from the disk and place it into the buffer. The buffer is used to provide efficiency for the read operations.
  • It sets up a character pointer which points to the first character of the file.

Consider the following example which opens a file in write mode.

Output

The content of the file will be printed.

Closing File: fclose()

File handling program in dev c template

The fclose() function is used to close a file. The file must be closed after performing all the operations on it. The syntax of fclose() function is given below:

C fprintf() and fscanf()

C fprintf() and fscanf() example

C fputc() and fgetc()

C fputc() and fgetc() example

C fputs() and fgets()

C fputs() and fgets() example

C fseek()

File Handling In Cpp

C fseek() example

Dev C++ Programs