optimizing function

Discussion in 'C++' started by puneet vyas, Feb 26, 2009.

  1. puneet vyas

    puneet vyas Guest

    hello, this is the function i wrote for making a series of directory
    and subdirectory,is there any optimized way of doing the operation
    which i am doing,like using string functions so tht without iterating
    over input string path
    BOOL DirectoryDetail::CreateSubDirectory(string &dirpath)
    {
    // File Handle
    HANDLE fFile;
    // File Information Structure
    WIN32_FIND_DATA fileinfo;
    // BOOL used to test if Create Directory was successful
    BOOL test;
    // Counter
    int x1 = 0;
    // Temporary string Object
    string tem = "";
    //check Does the file path already exists exist
    fFile = FindFirstFile(dirpath.c_str(),&fileinfo);
    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    //create directory heirarchy
    else
    {
    int length=dirpath.length();
    // Parse the supplied string Directory String
    for(x1=0;x1<length;x1++)
    {
    if(dirpath.at(x1) != '\\')
    // if the Charachter is not a \
    // else add character to temp string
    tem += dirpath.at(x1);
    else
    {
    //create directory one level by level
    test = CreateDirectory(tem.c_str(),NULL);
    if(test)
    SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);
    // if the Character is a \ Add the temp string to the vector of string
    tem += "\\"; // Now add the \ to the temp string
    }
    }
    // Close the file
    FindClose(fFile);
    // Now lets see if the directory was successfully created
    fFile = FindFirstFile(dirpath.c_str(),&fileinfo);
    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    else
    {
    FindClose(fFile);
    return FALSE;
    }
    }
    }
    puneet vyas, Feb 26, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nickolay Kolev

    Optimizing a text statistics function

    Nickolay Kolev, Apr 21, 2004, in forum: Python
    Replies:
    13
    Views:
    545
    Terry Reedy
    Apr 22, 2004
  2. Jack
    Replies:
    2
    Views:
    325
  3. puneet vyas

    optimizing function

    puneet vyas, Feb 26, 2009, in forum: C++
    Replies:
    0
    Views:
    484
    puneet vyas
    Feb 26, 2009
  4. carl

    Optimizing a function?

    carl, Dec 12, 2009, in forum: C++
    Replies:
    10
    Views:
    474
    joseph cook
    Dec 13, 2009
  5. Optimizing pow() function

    , Apr 22, 2013, in forum: C Programming
    Replies:
    45
    Views:
    748
    Seebs
    Apr 26, 2013
Loading...

Share This Page