i use visul c++ version "6"
please can any body help me for that
thank you
*/
#include <windows.h>
#include "wchar.h"
#include <STDIO.h>
#include <string.h>
int _scprintf(const char* format, ...){
va_list paramList;
va_start(paramList, format);
int size = vprintf(format, paramList);
va_end(paramList);
return size;
}
char *create_tab_buffer(int n)
{
char *result = new char[n + 1];
for(int i = 0; i < n; i++)
result[i] = '\t';
result[n] = 0;
return result;
}
class XMLFile
{
public:
XMLFile(char *pszFilename);
~XMLFile();
void openNewDirectoryLevel(char *pszName);
void closeCurrentDirectoryLevel();
void addNewFile(char *pszName, int size);
private:
HANDLE m_hFile;
int m_nTabs;
};
XMLFile::XMLFile(char *pszFilename)
{
char szBuffer[] = "<?xml version=\"1.0\"?>\n";
unsigned long dwWritten;
m_hFile = CreateFile(pszFilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(m_hFile, szBuffer, strlen(szBuffer), &dwWritten, NULL);
m_nTabs = 0;
}
XMLFile::~XMLFile()
{
CloseHandle(m_hFile);
}
void XMLFile::openNewDirectoryLevel(char *pszName)
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;
pTabBuffer = create_tab_buffer(m_nTabs);
iLength =_scprintf("%s<directory name=\"%s\">\n", pTabBuffer,
pszName) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s<directory name=\"%s\">\n", pTabBuffer,
pszName);
WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);
delete[] pszBuffer;
delete[] pTabBuffer;
m_nTabs++;
}
void XMLFile::closeCurrentDirectoryLevel()
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;
pTabBuffer = create_tab_buffer(--m_nTabs);
iLength = _scprintf("%s</directory>\n", pTabBuffer) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s</directory>\n", pTabBuffer);
WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);
delete[] pszBuffer;
delete[] pTabBuffer;
}
void XMLFile::addNewFile(char *pszName, int size)
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;
pTabBuffer = create_tab_buffer(m_nTabs);
iLength = _scprintf("%s<file name=\"%s\" size=\"%d\"/>\n",
pTabBuffer, pszName, size) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s<file name=\"%s\" size=\"%d\"/>\n", pTabBuffer,
pszName, size);
WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);
delete[] pszBuffer;
delete[] pTabBuffer;
}
char *getCurrentDir()
{
char *pszCurrentDir;
int length;
length = GetCurrentDirectory(0, NULL);
pszCurrentDir = new char[length + 1];
GetCurrentDirectory(length + 1, pszCurrentDir);
return pszCurrentDir;
}
void traverse(XMLFile *pOut, char *directory)
{
char *pszCurrentDir = new char[strlen(directory) + 3];
WIN32_FIND_DATA wfd;
HANDLE hFind;
pOut->openNewDirectoryLevel(directory);
sprintf(pszCurrentDir, "%s\\*", directory);
hFind = FindFirstFile(pszCurrentDir, &wfd);
if(hFind != INVALID_HANDLE_VALUE) {
do {
if(strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName, "..")) {
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
char *pszCurrentDir = new char[strlen(directory) + strlen
(wfd.cFileName) + 2];
sprintf(pszCurrentDir, "%s\\%s", directory, wfd.cFileName);
traverse(pOut, pszCurrentDir);
delete[] pszCurrentDir;
} else {
pOut->addNewFile(wfd.cFileName, wfd.nFileSizeLow);
}
}
} while(FindNextFile(hFind, &wfd) != 0);
}
pOut->closeCurrentDirectoryLevel();
FindClose(hFind);
delete[] pszCurrentDir;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)
{
XMLFile outc("c:\\d.xml");
char *pszCurrentDirc = "D:\\";
traverse(&outc, pszCurrentDirc);
while(1){Sleep(200);}
return 0;
}
int num_of_ints = 2;
int *pi = (int *)malloc(num_of_ints); // should be num_of_ints * sizeof(
int )
for( int x = 0; x < num_of_ints; x++ )
pi[x] = 1234;
free(pi); // Error will occur here, you have overflowed
You may want to set breakpoints at your delete[] statements and step over
them. Alternatively run in debug mode and when the application breaks you
can step up through the stack to find which line in your code triggers the
break.
You may find it useful to artificially inflate each buffer one at a time (eg
new char[iLength + 10000]) to see if it is an overflow error.
"Sa3Q" <s3q...@gmail.com> wrote in message
news:bca74b9a-d828-4836...@r24g2000yqd.googlegroups.com...
Don't know if this will compile with MSVC 6.0 (hey, it's time to upgrade!), but
I translated your mostly-C code to C++.
It's not how I'd done it, but it's a fairly direct translation.
Note how it's shorter, simpler and works:
<code>
#include <windows.h>
#include <string>
#include <fstream>
std::string tabs( int n ) { return std::string( 4*n, ' ' ); }
class XMLFile
{
public:
XMLFile( std::string const& name );
void openNewDirectoryLevel( std::string const& name );
void closeCurrentDirectoryLevel();
void addNewFile( std::string const& name, int size);
private:
std::ofstream m_file;
int m_nTabs;
XMLFile( XMLFile const& ); // No such.
XMLFile& operator=( XMLFile const& ); // No such.
};
XMLFile::XMLFile( std::string const& name )
: m_file( name.c_str() )
, m_nTabs( 0 )
{
m_file << "<?xml version=\"1.0\"?>\n";
}
void XMLFile::openNewDirectoryLevel( std::string const& name )
{
m_file << tabs( m_nTabs ) << "<directory name=\"" << name << "\">\n";
++m_nTabs;
}
void XMLFile::closeCurrentDirectoryLevel()
{
--m_nTabs;
m_file << tabs( m_nTabs ) << "</directory>\n";
}
void XMLFile::addNewFile( std::string const& name, int size )
{
m_file << tabs( m_nTabs ) << "<file name=\"" << name << "\"/>\n";
}
std::string getCurrentDir()
{
int const length = GetCurrentDirectory(0, NULL);
std::string result = std::string( length + 1, '\0' );
GetCurrentDirectory( length + 1, &result[0] );
result.resize( length );
return result;
}
void traverse( XMLFile& out, std::string const& directory )
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
out.openNewDirectoryLevel( directory );
hFind = FindFirstFile( (directory + "\\*").c_str(), &wfd );
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
if( strcmp(wfd.cFileName, ".") && strcmp( wfd.cFileName, ".." ) )
{
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
traverse( out, directory + "\\" + wfd.cFileName );
}
else
{
out.addNewFile( wfd.cFileName, wfd.nFileSizeLow );
}
}
} while( FindNextFile(hFind, &wfd ) != 0 );
}
out.closeCurrentDirectoryLevel();
FindClose(hFind);
}
int main()
{
XMLFile outc( "d.xml" );
traverse( outc, "c:\\test" );
}
</code>
Cheers & hth.,
- Alf
this code not run :(
If you want help with that then you'll have to supply the relevant information.
thank you the code run perfect on msvc 6
i try to compile it on
msvc 9
but it not run
thank you thank you