Monday, 3 March 2014

Getting files information in directory.

The below code useful, when
   1. When we want to list "FileNames"
   2. To know which files are having 100KB size
   3. To rename files in a directory.

#include <afxwin.h>

bool IsDots(const TCHAR* str)
{
    if(_tcscmp(str, ".") && _tcscmp(str, "..")) return FALSE;
    return TRUE;
}


BOOL GetDirectoryDetails(const TCHAR* sPath)
{
    HANDLE hFind;    // file handle
    WIN32_FIND_DATA FindFileData;
   
    TCHAR DirPath[MAX_PATH];
    TCHAR FileName[MAX_PATH];
   
    _tcscpy(DirPath,sPath);
    _tcscat(DirPath,"\\*");    // searching all files
    _tcscpy(FileName,sPath);
    _tcscat(FileName,"\\");
   
    // find the first file
    hFind = FindFirstFile(DirPath,&FindFileData);
    if(hFind == INVALID_HANDLE_VALUE)
        return FALSE;

    _tcscpy(DirPath,FileName);
   
    bool bSearch = true;
    while(bSearch)
    {    // until we find an entry
        if(FindNextFile(hFind,&FindFileData))
        {
            if(IsDots(FindFileData.cFileName))
                continue;           
           
            FileName[0]='\0';
            _tcscat(FileName,FindFileData.cFileName);

            //check whether this file is a directory or normal file
            if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {   
                CString dirName;
                dirName.Format("%s\\%s",DirPath,FileName);
               
                // we have found a directory, recurse
                if(!GetDirectoryDetails(dirName))
                {
                    FindClose(hFind);
                    return FALSE;   
                }           
            }
            else
            {
                CString dirFullPath(DirPath);
                CString parentDirName;
               
                //First remove last '\'
                if(dirFullPath.GetLength() == dirFullPath.ReverseFind('\\') + 1)
                    dirFullPath.Delete(dirFullPath.ReverseFind('\\'),1);
               
                int pos = dirFullPath.ReverseFind('\\');

                parentDirName = dirFullPath.Mid(pos+1, dirFullPath.GetLength() - pos);
               
                CString csOldFileName,csNewFileName;
                csOldFileName.Format("%s\\%s",dirFullPath,FileName);

                //insert parentDirName before extension
                CString temp(FileName);
                CString temp2;
                temp2.Format("_%s",parentDirName);
                temp.Insert(temp.ReverseFind('.'),temp2.operator LPCTSTR());           

                csNewFileName.Format("%s\\%s",dirFullPath,temp);
                   
                //Rename
                if(rename(csOldFileName,csNewFileName) == 0)
                    int debug=0;
            }
        }
        else
        {
            // no more files there
            if(GetLastError() == ERROR_NO_MORE_FILES)
                bSearch = false;
            else {
                // some error occurred; close the handle and return FALSE
                FindClose(hFind);
                return FALSE;
            }
           
        }
       
    }
    FindClose(hFind);                  // close the file handle
   
    return true;
   
}

How to Format GetLastError code?


   When we donot know the reason why the statement is failing. Then call below function:
        DWORD errorCode=GetLastError();

   Above function will return an error code. We can translate that code into string as below.
        TCHAR errorMessage[1024] = TEXT("");
       
        DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM
            | FORMAT_MESSAGE_IGNORE_INSERTS
            | FORMAT_MESSAGE_MAX_WIDTH_MASK;
       
        FormatMessage( flags,
            NULL,
            errorCode,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            errorMessage,
            sizeof(errorMessage)/sizeof(TCHAR),
            NULL );

Tuesday, 19 February 2013

Inheritance in c++

Inheritance:

         One of the most important concepts in object-oriented programming is that of inheritance. Inheritance is the process of transmitting properties of one class into another. Class which is giving properties is called "Super class" and class which is acquiring the properties is called "Sub class".

        Super class can be called as "Base class" or "Parent class" or "Ascendent class".
        Sub class can be called as "Derived class" or "Child class" or "Descendent class".

So, The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

   Importance of Inheritance

         Inheritance was added in C++ for many reasons: 
         Code Re-usability: Inheritance helps us reduce the amount of code and the number of lines of code we have to write. If we want creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class.
 
   Things those are not inherited 
  1. Constructor
  2. Destructor
  3. Copy constructor
  4. Assignment operator
  5. friendship 

Types of Inheritance 

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

    Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

    Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

    Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.