Thursday, May 21, 2009

Recursive Method to list all the file present in a Directory using cpp



#include < iostream >
#include < dirent.h >

using namespace std;

class recDir
{
private:
int fileCount;
public:
recDir()
{
fileCount = 0;
}
void showFileList(char *dirPath)
{
char *subDir="",*temp;
DIR *curDir;
struct dirent *entry;
if(curDir = opendir(dirPath))
{
while(entry = readdir(curDir))
{
if(strcmp(entry- > d_name,".") != 0 &&
strcmp(entry- > d_name,"..") != 0)
{
temp=strstr(entry- > d_name,".");
if(temp!=NULL
{
fileCount++;
cout << "File [" <<
fileCount << "] " <<
entry- > d_name << endl;
}
else
{
subDir = dirPath;
strcat(subDir,"/");
strcat(subDir,entry- > d_name);
showFileList(subDir);
}
}
}
closedir(curDir);
}
else
cout << "Error:Could not open directory/file\n";
}
int totalFiles()
{
return fileCount;
}
};


int main(int argc,char *argv[])
{
recDir obj;
obj.showFileList(argv[1]);
cout << endl << "Total Files =
" << obj.totalFiles() << endl;
}

Simple Example to extract data from xml file using expat............

takes an xml file and extract attributes into a file .rtf


#include "stdio.h"
#include "expat.h"
#include"string.h"
#define BUFFSIZE 8000



FILE *rtfFile;


static void startElement(void *cargo, const char *el,const char **attr)
{
}
static void endElement(void *cargo, const char *el)
{
}
static void characters(void *cargo, const char *ch, int len)
{
int i;
for (i=0; i putc(ch[i],rtfFile);
}

int main(int argc, char *argv[])
{
if(argc!=3)
{
printf("\nUsage : \n");
exit(0);
}
char Buff[BUFFSIZE];
FILE *fp;

fp = fopen(argv[1],"r");
rtfFile = fopen(strcat(argv[2],".rtf"),"w");

XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, characters);

for (;;)
{
int len = fread(Buff, 1, BUFFSIZE, fp);
int done = feof(fp);
if (! XML_Parse(parser, Buff, len, done))
exit(-1);
if (done) break;
}
fclose(fp);
printf("\n");
fclose(rtfFile);
return 0;
}


for Example we can use following content of a

< ?xml version="1.0"?>
< story>
< storyinfo>
< author>Ranjeet Kumar</author>
< datewritten>October 15,1985 </datewritten>
< keyword>Don</keyword>
< /storyinfo>
< body >
< headline > It's not gonna to help you < /headline >
< para > Don't be fool < /para >
< /body >
< /story >

Tuesday, May 19, 2009

Extracting data from the XML files Using expat on linux (XML parser)

1)Download .tar.gz files on from

http://webscripts.softpedia.com/script/Development-Scripts-js/XML-Tools/Expat--26466.html

2)extract into a folder
3) Move to this folder and run following well known commands

    ./configure
    make
    make install

4)now the times comes to check weather it is working or not.............
for this purpose expat people are very good as they have given two examples (too much i guess) in the folder expat.version/examples/. what you have have to do for checking, obviously copy a xml file from any where as you wish and run these programs.......
$>gcc examples.c -l expat
$>./a.out < xmlfile.xml

i hope you will see something which you need.

Search Ranjeet's Blog