Using Pointers and performing file manipulations using C++


14 Mar  

This edition is a part of the introductory tutorial segment that I started recently.  Please go the C/C++ category to see the previous tutorials. In this edition, I will show how to perform simple file operations like creating a new file, writing to a file, reading data from a file etc. We will also be addressing the issues associated with pointers in C++.

 

Create a file and write data

 

Let me show you the code first:

 

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    ofstream test;
    test.open("test.txt");
    test << "Sample Tutorial for Techblog";
    test.close();
    return 0;
}

 

Here you can see that I am using a library (header) called fstream. This is required for performing file manipulations. The object ‘test’, which is the instance of the class ‘ofstream’, can access all the functions required for  writing data to the file.

In this case, it opens a file called ‘test.txt’ (if the file does not exist, it will create the file) and saves the string to it. Finally, we safely close the file (to avoid errors).  When you execute this code, you will get an output similar to the one shown below:

 

write data to file in C++

 

Reading a file

Just like we wrote data to the file, we can read data from a file using an object created as the instance of the class ‘ifstream’.  Here is code:

 

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    char file[10];
    cin.getline(file,10);
    ifstream test;
    test.open(file);

    char word[10];
    test >> word;
    while(test.good())
    {
        cout << word << " ";
        test>>word;
    }

    return 0;

}

 

The getline function will offer the cursor to the user and takes input. As you can see, we have put a restriction on the size of the file name (10). This is always a good idea to include in the code. Then, it opens the file and reads the words. Then, we use a loop to check whether we have reached the end of the file (using the function good() ). You can improvise this code by checking if you are able to open the file or not. If it fails to open, you can return an error (ERROR_FAIL). Only thing that you need to note here is that you need to include the cstdlib header file for using this function (to return the error).

 

Here is the code in action:

 

read data from file in C++

 

Memory location and pointers

C++ allows us to see the memory location and use pointers to access them. Using this piece of code you can see the memory location of two variables.

 

#include <iostream>

using namespace std;

int main ()
{
    double a;
    int i;
    a=1.234;
    i=7;

cout << &a <<endl;
  cout << &i <<endl;

    return 0;

}

 

You  may note that C++ allocates memory location when you define the variables. And you can easily access them by using ‘&variable_name’. See the screenshot given below (you can see the memory location in hexadecimal):

 

using pointers and getting memory locations in C++

 

You can use pointers (which are used in the format – *pointer_name ) to access these memory locations. But there are many security issues associated with them. This is the reason why Java is not using pointers.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Tags: , ,


TechBlog on Facebook

Leave a Reply