Tuesday, May 04, 2010

Learning C







I felt that I needed to learn something new and exciting, then I asked my friends for direction. I was told to try C language. Wow, I was thrilled by that. One of my friends even gave me a great C book to start out with. I was also given a long list of books to read. However, I was not totally ready to dive into C until I met a hacker called Jari Komppa. That is how my life and experience with C changed. He was like an angel, ever willing to help and above all extremely brilliant.

He gave me a little project to start off with C, could you guess what the project was? Hmm, don't worry here is the answer; To traverse a folder and list all the files with their sizes. I figured out that with C activation record I could easily build up a stack. That was what I did, and pow, I got the desired result. It may not be the best, pretty crude, but I was happy with it.Well, I must confess that I was not able to carry out the task until I got my fingers burnt while studying Algorithms.

#include
#include
#include
#include

void processSize(char *name){
FILE * pFile;
pFile = fopen ( name , "rb" );

fseek ( pFile , 0L , SEEK_END );
long sz = ftell(pFile);
//fputs ( " sam" , pFile );
fclose ( pFile );
printf ("size = %ld bytes",sz);
}

void processFile(char gPath[]){
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char *gCat;
char *gin;
char *cot;
char *cat;
char *fCat;
char *tempChr;
tempChr = (char *)malloc(strlen(gPath) * sizeof(char *));
strcpy(tempChr,gPath);
hFind = FindFirstFile(tempChr, &FindFileData);

while(FindNextFile(hFind, &FindFileData)){

gCat = strchr(tempChr,'*');
int inValue = strlen(tempChr) - strlen(gCat);

cot = (char *)(malloc(inValue * sizeof(char *)));
strncpy(cot,tempChr,inValue);
strcat(cot, FindFileData.cFileName);

if (!((strcmp(FindFileData.cFileName, ".")== 0)||
(strcmp(FindFileData.cFileName, "..")== 0))){

if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// printf("future started yesterday");
printf("----%s----\n",FindFileData.cFileName);

strcat(cot,"\\*.*");
processFile(cot);
printf("---End-%s---\n",FindFileData.cFileName);
}
else{
printf("\t\t|--%s--",FindFileData.cFileName);
// printf("ooo%s",cot);
processSize(cot);
printf("\n");
}
}
}
}

int _tmain(int argc, TCHAR *argv[])
{


processFile("C:\\Documents and Settings\\idea\\Desktop\\60-h\\*.*");
//Put your the directory of your target here, note it is assumed you are using windows.

return 0 ;
}



I invite your comments

No comments:

Tags

Arduino (1) C (3) Clojure (3) Perl (1) the other side (8) VBA (1)

micro's shared items