optimizing function

P

puneet vyas

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,i mean using string functions without iterating over
string

// recursive directories.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
//#include "WriteDir.h"
#include<stdio.h>
#include <afx.h>
#include <CString>
#include <direct.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

/*
BOOL WriteDirectory(CString dd)
{


HANDLE fFile; // File Handle
WIN32_FIND_DATA fileinfo; // File Information Structure
CStringArray m_arr; // CString Array to hold Directory
Structures
vector<string> paths;
BOOL tt; // BOOL used to test if Create
Directory was successful
int x1 = 0; // Counter
CString tem = ""; // Temporary CString Object
string temporary="";
// Before we go to a lot of work.
// Does the file exist

fFile = FindFirstFile(dd,&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;
}

m_arr.RemoveAll();
int length=dd.GetLength();
string dir=dd;
int length1=dir.length();
// Not really necessary - Just habit
for(x1=0;x1<length;x1++) // Parse the supplied CString Directory
String
{
if(dd.GetAt(x1) != '\\') // if the Charachter is not a \
tem += dd.GetAt(x1); // else add character to Temp String
else
{
paths.push_back()
m_arr.Add(tem); // if the Character is a \ Add the Temp
String to the CString Array
tem += "\\"; // Now add the \ to the temp string
}
if(x1 == length-1) // If we reached the end of the file add the
remaining string
m_arr.Add(tem);
}


// Close the file
FindClose(fFile);
int size=m_arr.GetSize();
// Now lets cycle through the String Array and create each directory
in turn
for(x1=1;x1<size;x1++)
{
tem = m_arr.GetAt(x1);
tt = CreateDirectory(tem,NULL);

// If the Directory exists it will return a false
if(tt)
SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
// If we were successful we set the attributes to normal
}
m_arr.RemoveAll();
// Now lets see if the directory was successfully created
fFile = FindFirstFile(dd,&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;
}
}

*/

BOOL WriteDirectory(string dd)
{

HANDLE fFile; // File Handle
WIN32_FIND_DATA fileinfo; // File Information Structure
// CString Array to hold Directory Structures
//vector<string> paths;
BOOL tt; // BOOL used to test if Create
Directory was successful
int x1 = 0; // Counter
string tem = ""; // Temporary CString Object

// Before we go to a lot of work.
// Does the file exist

fFile = FindFirstFile(dd.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;
}

//m_arr.RemoveAll();
int length=dd.length();


for(x1=0;x1<length;x1++) // Parse the supplied CString
Directory String
{
if(dd.at(x1) != '\\') // if the Charachter is not a \
tem += dd.at(x1); // else add character to Temp
String
else
{

tt = CreateDirectory(tem.c_str(),NULL);
if(tt)
SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);

// if the Character is a \ Add the Temp String to the
CString Array
tem += "\\"; // Now add the \ to the temp
string
}

if(x1 == length-1) // If we reached the end of the file
add the remaining string
{
// paths.push_back(tem);
tt = CreateDirectory(tem.c_str(),NULL);
if(tt)
SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);
}

}


// Close the file
FindClose(fFile);


// Now lets see if the directory was successfully created
fFile = FindFirstFile(dd.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;
}
}



int _tmain(int argc, _TCHAR* argv[])
{
//string Fullpath[];
//_mkdir ();
int IsDirectoryCreated=WriteDirectory("c:\\puneettest1\\test2");

if(IsDirectoryCreated)
{

cout<<"dir created\n";
}
char buffer[3400];
char *p=buffer;
int i;

i=::GetCurrentDirectory(3400,p);
if(i==0)
{
printf("sdasd");
}


/*if(IsDirectoryCreated==0)
{

}*/

cout<<p<<endl;
return 0;
}



// recursive directories.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
//#include "WriteDir.h"
#include<stdio.h>
#include <afx.h>
#include <CString>
#include <direct.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

/*
BOOL WriteDirectory(CString dd)
{


HANDLE fFile; // File Handle
WIN32_FIND_DATA fileinfo; // File Information Structure
CStringArray m_arr; // CString Array to hold Directory
Structures
vector<string> paths;
BOOL tt; // BOOL used to test if Create
Directory was successful
int x1 = 0; // Counter
CString tem = ""; // Temporary CString Object
string temporary="";
// Before we go to a lot of work.
// Does the file exist

fFile = FindFirstFile(dd,&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;
}

m_arr.RemoveAll();
int length=dd.GetLength();
string dir=dd;
int length1=dir.length();
// Not really necessary - Just habit
for(x1=0;x1<length;x1++) // Parse the supplied CString Directory
String
{
if(dd.GetAt(x1) != '\\') // if the Charachter is not a \
tem += dd.GetAt(x1); // else add character to Temp String
else
{
paths.push_back()
m_arr.Add(tem); // if the Character is a \ Add the Temp
String to the CString Array
tem += "\\"; // Now add the \ to the temp string
}
if(x1 == length-1) // If we reached the end of the file add the
remaining string
m_arr.Add(tem);
}


// Close the file
FindClose(fFile);
int size=m_arr.GetSize();
// Now lets cycle through the String Array and create each directory
in turn
for(x1=1;x1<size;x1++)
{
tem = m_arr.GetAt(x1);
tt = CreateDirectory(tem,NULL);

// If the Directory exists it will return a false
if(tt)
SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
// If we were successful we set the attributes to normal
}
m_arr.RemoveAll();
// Now lets see if the directory was successfully created
fFile = FindFirstFile(dd,&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;
}
}

*/

BOOL WriteDirectory(string dd)
{

HANDLE fFile; // File Handle
WIN32_FIND_DATA fileinfo; // File Information Structure
// CString Array to hold Directory Structures
//vector<string> paths;
BOOL tt; // BOOL used to test if Create
Directory was successful
int x1 = 0; // Counter
string tem = ""; // Temporary CString Object

// Before we go to a lot of work.
// Does the file exist

fFile = FindFirstFile(dd.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;
}

//m_arr.RemoveAll();
int length=dd.length();


for(x1=0;x1<length;x1++) // Parse the supplied CString
Directory String
{
if(dd.at(x1) != '\\') // if the Charachter is not a \
tem += dd.at(x1); // else add character to Temp
String
else
{

tt = CreateDirectory(tem.c_str(),NULL);
if(tt)
SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);

// if the Character is a \ Add the Temp String to the
CString Array
tem += "\\"; // Now add the \ to the temp
string
}

if(x1 == length-1) // If we reached the end of the file
add the remaining string
{
// paths.push_back(tem);
tt = CreateDirectory(tem.c_str(),NULL);
if(tt)
SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);
}

}


// Close the file
FindClose(fFile);


// Now lets see if the directory was successfully created
fFile = FindFirstFile(dd.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;
}
}



int _tmain(int argc, _TCHAR* argv[])
{
//string Fullpath[];
//_mkdir ();
int IsDirectoryCreated=WriteDirectory("c:\\puneettest1\\test2");

if(IsDirectoryCreated)
{

cout<<"dir created\n";
}
char buffer[3400];
char *p=buffer;
int i;

i=::GetCurrentDirectory(3400,p);
if(i==0)
{
printf("sdasd");
}


/*if(IsDirectoryCreated==0)
{

}*/

cout<<p<<endl;
return 0;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top