Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: File I/O

C++ Programming Series: File I/O

In this post, we are going to discuss input and output. No, I am not talking about cout, cin or any other methods we have discussed till now. I am talking about writing and reading files.

For working with files, we need to include another header called fstream.

There are three classes in it, for file I/O:

  • ofstream – Class for writing files
  • ifstream – Class for reading files
  • fstream – Class for writing as well as reading files

We are going to discuss ofstream and ifstream only. fstream is a bit different to use but who cares! We don’t need that!

The codes for file I/O are present below. It is pretty simple and there is more comment in the code than the code itself, for explaining things!

File Writing:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    //The 'ofstream' class for writing a file
    ofstream fileWriter("a_file.txt");

    int number = 21;

    //Writing the stuff
    fileWriter << number;
    fileWriter << " ";
    fileWriter << 13;

    //Closing that file which is opened; it is important!
    fileWriter.close();

    return 0;
}

File Reading:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    //The 'ifstream' class for reading a file
    ifstream fileReader;

    //Opening a file called 'a_file.txt' in local directory
    fileReader.open("a_file.txt");

    //For checking whether the file fails to open or not
    if (fileReader.fail())
        cout << "There is no file in the address!" << endl;

    //While the file that is opened, has some context
    //in it at the pointer location
    while (fileReader.good())
    {
        //Variable to store reading from the file
        int number;

        //Reading the number at the pointer location and then,
        //proceeding the pointer forward for further reading
        fileReader >> number;

        //Displaying the number present in the file
        cout << number << endl;
    }

    //Closing that file which is opened; it is important!
    fileReader.close();

return 0;
}

Reading and writing a file requires us to have the concept of a pointer. The pointer points to someplace in the file which then, proceeds forward with every single read/write operation. That pointer is similar to the one which appears at the time of typing.

I hope that you understood how to read and write a file. But there are still some doubts and confusions!

The first one is the formatting of the file that we are reading. White spaces are something that no one wants to read/get from the file. So, ifstream don’t consider these spaces and no matter how much white space there is, it will be considered as one space only.

Of course, we can’t just read data as discrete if that data is arranged without spaces in between. For example, if the file contains two words without any space like this, “nospace”, ifstream reads it as “nospace” and so, giving space between them rectifies the issue.

The second one is the data type for reading and writing a file. We can use all primitive types. We can use strings as well if we include the string header.

The third one is formatting a file being written. We can have formatting by just writing in character literals like \t or \n to have a tab or a new line respectively.

This is how we simply read and write files. Now, your task is to modify the registration program that we made with functions previously, to write a file with the data inputted by the user.

Soon we will see, how much fun is programming! It is fun for me as I am developing a game in C++ called SamuraiDuel.

Leave a Reply

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