Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Writing an application on Basic File Operations Using Win32 System Services

Author: DevASP
Download Source Code :

Files are one of the fundamental services provided by every OS. A file is just a Sector on the disk which have a unique name. But in programming we deal files just like a buffer on an abstract level.

Some API provides the ability to read these sectors on the disk in character mode. We will talk in this article Windows API’s which OS provide as its OS Services.Accordance to the Windows 32 API, a file can be think as a collection of Bytes.Files can be stored a character format or as binary format in which data is stored o The disk as a chunk of structures. Readfile and WriteFile these both API can be use to read and write both type of files either Character mode or Binary mode.


These both api’s can be used to write both on communication port and the networks.Suppose we want to write a simple Application in which we will Open a file and read some data from the file and then we will close the file. But remember that we will use only the API’s provided by the System Services.

We can write this code in C language using the stdio library functions like this.



#include <windows.h>

#include <stdio.h>


int main()



   char myfile[PATH_SIZE];

   char charaterToRead;

   FILE *file;

   printf("input the filename to program");



  file = fopen(myfile,"r");//open the file in read mode

   if (file)

      // read until end of file

      while ((charaterToRead = fgetc(file)) != EOF)

         printf("%c", charaterToRead);





This program simply open a file in read mode read each character from he file and display them on the screen.OK let’s try some object orientation If we have to do the entire above thing using Object oriented library functions provided by C++ this will look like this



#include <windows.h>

#include <iostream.h>

#include <fstream.h>


void main()


   char myfile[PATH_SIZE];

   char charaterToRead;


   cout << " input the filename to program ";

   cin >> myfile;


  //open file in read mode using ifstrem object

  ifstream inputFileStream(myfile);

    if (inputFileStream)

      // read until end of file

      while (inputFileStream.get(charaterToRead))

//Get function read a character from the opened Stream.

        cout << charaterToRead;


   //Close function close the Opened Stream.




In above programs one thing is common Program just ask the user a file name then it print all the file on the screen. Now the Most Important thing, If we want to use the low level API’s, We just have to learn three functions which are




This program Gets a file name then read it to the EOF(this represent the end of file which is equal to -1) and then close the file.The source code provided with this Article has this code in the Button clicked event.



HANDLE HandleToFile;

      BOOL IsSuccess;

      DWORD Num;

      char s;

      char MyFile[]="testdata.txt";

      char data1[100];

      // Open the file

      HandleToFile = CreateFile(MyFile, GENERIC_READ,

            0, 0, OPEN_EXISTING, 0, 0);

      if (HandleToFile == INVALID_HANDLE_VALUE)


            MessageBox("Cant open the file","error");



      int counter=0;



            IsSuccess = ReadFile(HandleToFile, &s, 1, &Num,0);



      while (Num>0 && IsSuccess);







CreateFile function open the file for read and write operations.

The signature of this function looks like


HANDLE CreateFile(

   LPCTSTR name,

   DWORD accessMode,

   DWORD shareMode,

   LPSECURITY_ATTRIBUTES securityAttributes,

   DWORD create,

   DWORD attributes,

   HANDLE templateFile)


Name                            Name of the file to open for reading and writing.

accessMode                  Read/Write mode

shareMode                    The way the file should be shared

securityAttributes           Address of a security structure.

Create                           The way the file should be created

Attributes                      Settings for file attribute bits and flags

templateFile                   File containing extended attributes


This function accepts the file name and mode such as GENERIC_READ Access mode,its mean that fily will be strictly used in read mode.


OPEN_EXISTING means file already exist.


This function returns a Handle to the file which is supposed to be open.After opening the file the ReadFile function is used to read a single character at atime from the file.We can pass it a buffer and the number of bytes to read so in this way it works like a generic function.The function signature looks like this.


BOOL ReadFile(

   HANDLE file,

   LPVOID buffer,

   DWORD requestedBytes,

   LPDWORD actualBytes,

   LPOVERLAPPED overlapped)


ReadFile function Returns TRUE on success.

In the end the CloseHandle() close the handle which is opened by the Create File to read or write to/fromfile.The signature of this function looks like this


BOOL CloseHandle(HANDLE hObject);

 CloseHandle()Returns TRUE on success


One thing I must want to mention here that when we talk to use windows services,clear one thing thing in your mind that OS take care of its every resource through HANDLES. Files are also resource for OS so that’s why we can access them through handle.

Add Article Comment:
Name :
Email Address :
Comments :
<< Limiting Multiple Field Length on Web Form.

Disclaimer - Privacy
© 2002-2017