Thursday, December 15, 2011

Program to read the contact details from collection of VCF files:

After so many days, my techBlog is getting a post, Great.

Recently i baught a new lower end SAMSUMNG phone. I felt good to have that phone, Samsung Metro C3322. My phone is having an option to take the backup of messages, contacts, calendars etc. May be other phones also have. When i took the backup of contacts, .vcf files are created for each contact. I knew something about vcf files due to my project experience.

After few days, a thought came to my mind to take the print out of all the contacts of my phone. But files are more which is of indivual. Then, i decided to write a C program to extract the names and numbers of all the contacts in a directory and started immediately.

Using file I/O and string functions, i could finish as early as possible.

When i was in KPIT, i used strtok() function a lot which made me not to forget. Once again, this program made me to memorise the same. Really, strtok() function is tood good for us like Application programmers.


My program will take directory of vcf files as input and it will write the names and numbers (mobile and home only) into a file, NameNumber.

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include
#include
#include

int writeToFile(char *data)
{
FILE *fp;
fp = fopen("NameNumber", "a");
if(fp == NULL)
{
printf("fopen error in function");
exit(10);
}
fwrite(data, 1, strlen(data), fp);
fclose(fp);
return 0;
}


int readFile(FILE *fp2)
{
char *ptr, Number[50], FName[50], buf[80];
int CN, FN;
char data[100];

while(!feof(fp2))
{
if(fgets(buf, 50, fp2)!= NULL)
{
ptr=strtok(buf, ":;");
printf("string is : %s\n", ptr);
while(ptr != NULL)
{
if(FN == 1)
{
memset(&FName, 0, 50); ////Note:
strncpy(FName, ptr, strlen(ptr)-1);
FName[strlen(FName)-1]='\0';
FN=0;
printf("Name is : %s\n", FName);
}
if(strcmp(ptr, "FN")==0)
{
FN=1;
}
if(CN == 1)
{
if(strcmp(ptr, "VOICE")==0)
{
ptr = strtok(NULL, ":;");
}
strcpy(Number, ptr);
CN=0;
printf("Number is %s\n", Number);
}

if(strcmp(ptr, "CELL")==0)
{
CN=1;
strcpy(Number, ptr);
}
if(strcmp(ptr, "HOME")==0)
{
CN=1;
strcpy(Number, ptr);
}
ptr = strtok(NULL, ":;");
}
}
else
{
break;
}
}
sprintf(data,"%s %s %s", FName, ":", Number);
writeToFile(data);
}

int main()
{
FILE *fp, *fp2;
int FN=0, CN=0;
char fileName[50], *ptr=NULL, buf[30], temp[10], FName[30], Number[30];
char path[100];
system("ls myPhone/contact/ > file");
fp = fopen("file", "r");
if(fp == NULL)
{
printf("Fileopen error");
exit(10);
}
while(!feof(fp))
{
strcpy(path, "myPhone/contact/");
if(fgets(fileName, 100, fp) == NULL)
{
printf("fgets error for %s", fileName);
exit(10);
}
fp2 = fopen(strncat(path, fileName, strlen(fileName)-1), "r");
if(fp2 == NULL)
{
printf("Fileopen2 error: %s\n", path);
exit(10);
}
printf("Opened is : %s\n", path);
readFile(fp2);
}
exit(10);
fclose(fp);
fclose(fp2);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

In the above program, memset() function made this program more robust. Because, I was facing some issues related to names. When i was printing the names, i was getting some junk characters. I took quite amount of time to solve using memset function. You can try the same without memset function.

This was written and executed under Linux platform. But when i run the same program under QNX platform, program crashed and code dump is found. I am planning to dig into that to understand the behavior of QNX. Let me see...

I like to write simple programs frequently by having these kind of problem statements in hand which will make me to remember something good about this tech field.

Keep Programming, Keep Doing Good....