Friday, September 16, 2011

Deleting a File in C

Here is the program to delete a file in C......
#include <stdio.h>
#include<stdio.h>
#include<errno.h>
int main(void)
{
   char file[80];
   int i;
   clrscr();
   printf("File to delete: ");
   gets(file);
   i=remove(file);
   if(i==0)
   {
   printf("file removed");
   }
   else
   {
   if(errno==ENOENT)
   printf("\nNo such file exists");
   if(errno==EACCES)
   printf("\nPermission denied");
   }
   getch();
   return 0;
}
Feel Free to Comment if any point is not clear.........

Tuesday, September 13, 2011

Renaming a file in c

In this post we will find how to rename and move a file in the same drive using c language....
This can be done in c using the rename() function..The prototype of rename is:
int rename(const *char oldname,const *char newname);
On sucess--it returns 0,
On error---it returns -1;
Let us take an example:

#include <stdio.h>
#include<conio.h>
#include<errno.h>
int main(void)
{
   char oldname[80], newname[80];
   int i;
   clrscr();
   printf("Enter File to rename: ");
   gets(oldname);
   printf("\nEnter New name: ");
   gets(newname);
   i=rename(oldname, newname);
     if(i==0)
     {
     printf("rename done");
     }
     else
     {
     if(errno==ENOENT){printf("no such file/directory exists");}
     if(errno==EACCES){printf("access denied");}
     if(errno==ENOTSAM){printf("Drive is not same");}
     }
getch();
   return 0;
}
Let us find the output for different inputs
output:
Enter  File to rename:c:\test.txt
Enter New name:c:\go.txt
rename done
Explanation:it will sucessfully rename the file test to go in the c drive

output:
Enter  File to rename:d:\test.txt
Enter New name:c:\go.txt
Drive is not same
Explanation:Using rename function u can only work within a drive

output:
Enter  File to rename:d:\test.txt
Enter New name:d:\moveinthis\go.txt
rename done
Explanation:You can move the file from one directory to another directory using rename but the condition is that the directories must be in the same drive......

output:
Enter  File to rename:d:\ex.txt
Enter New name:d:\moveinthis\go.txt
no such file/directory exists
Explanation:This message will come if there is no file named as ex.txt present in the d drive......

output:
Enter  File to rename:c:\temp
Enter New name:c:\check
rename done
Explanation:We can rename also a directory  using the rename function,but still the condition is we must work in the same drive.....

Thursday, September 8, 2011

The difference between pointer to array and pointer to its first element

In the previous Post,We have seen that the address of array and the address of its first element is same,so,the question was what is the difference between Pointer to array and the Pointer to the first element of array while both contains the same address as proved in this Post....
That is if write something like this--
int (*ptr)[5];
ptr=&a;
and a pointer to first element of the array that is....
int *ptr1;

ptr1=a;
Then,What's the Difference between ptr and ptr1??????

To Get the difference between we have just to recall the concept of pointer arithmetic that is what happens if we add some number to a pointer??...
As we know whenever we add any number to the pointer the pointer appends to the memory location which is equal to the sizeof datatype*number....
For example:if a pointer is pointing to an int and its value is 65588 then if we add 1 to the pointer then it goes to 65590 that is (sizeof int *num) that is 2*1=2,so 2 gets added to the pointer and now it points to 65590 memory location...

So,Having the concept of pointer arithmetic in mind when we declare a pointer to an array and increments it the value of the pointer get incremented by sizeof array*datatype size*number...

For example:
if we write int a[5]={1,2,3,4,5};
the sizrof array is 5,
datatype size is 2 for int(16-bit assumed),
and number is 1,
int(*ptr)[5];
ptr=&a;
ptr=ptr+1;or ptr++;
if in address of array is 65516 that is ptr is 65516 in the beginning then after ptr=ptr+1 or ptr++;
The value of ptr is 65516+(5*2*1)=65516+10=65526
This is what happens if ptr is the pointer to array and if the ptr is the pointer to the first elements of the array then as the first element is an int so when we increments the ptr the value get incremented by sizeof int that is it points to the next element...
let us write a program 
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={1,2,3,4,5};
int(*ptr1)[5];
int *ptr2;
ptr1=&a;//pointer to an array
ptr2=&a[0];//pointer to first element of array
printf("The address of array is:%u",ptr1);
printf("\nThe address of first element of array is:%u",ptr2);
ptr1++;
printf("\nNow the value of ptr1 is:%u",ptr1);
ptr2++;
printf("\nNow the value of ptr2 is:%u"",ptr2);
}
output:
The address of array is:65516
The address of first element of array is:65516
Now the value of ptr1 is:65526
Now the value of ptr2 is:65518


I explained to the best of my knowledge,Correct me if there is anything wrong by commenting ,your comments,query and suggestions are invited to raise the level of this blog......
Have a nice time.....

Wednesday, September 7, 2011

Address of array and Address of first element of array

Today we will explore the address of array and the Address of First Element of the same array....
let us first create an array of int containing the 5 elements.
int a[5]={1,2,3,4,5};
Here a is the array of the 5 int data type elements...
As we know we can find the address of the array by applying the & operator with the array name that is "a",so the address of the aray is "&a"...we can print this address using the %u format specifier in the printf..
printf("address of array is:%u",&a);//address of the array
let us write a program..
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5};
clrscr();
printf("The address of array is:%u",&a);
getch();
}
The output is the address of the array...let us say it prints
The address of array is:65516
Now let us find the address the first element of the array,as ww know we can get the first element by a[0],and its address by &a[0]..
so the statement printf("The address of first element is:%u",&a[0]); gives the address of the first element of the array...it prints.
The address of first element is:65516
Further,we also know that the a(the name of the array) is nothing but the pointer to the first element of the array so printing the a will also give the same value....

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5};
printf("The address of array is:%u",&a);
printf("The address of first element is:%u",&a[0]);
printf("The value of a is:%u",a);
}
output:
The address of array is:65516
The address of first element is:65516
The value of a is:65516

As we can see the result the,The conclusion is...
"The Address of Array and the Address of its first element is Same........".
As for the explanation we can say that array starts from the first element so the address of its first elements is same as the address of itself........

So,the Question arises--
"What's the Difference Between Pointer to array and the pointer to the first element of array if both contains the same address????"
that is:
if we declare int a[5];
and a pointer to array that is...
int (*ptr)[5];
ptr=&a;
and a pointer to first element of the array that is....
int *ptr1;
ptr1=a;
so,What's the Difference between ptr and ptr1??????
Get Answer here.

Sunday, September 4, 2011

MAC address using c

Mac Address-->from Wikipedia
A Media Access Control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment. MAC addresses are used for numerous network technologies and most IEEE 802 network technologies including Ethernet. Logically, MAC addresses are used in the Media Access Control protocol sub-layer of the OSI reference model.
MAC addresses are most often assigned by the manufacturer of a network interface card (NIC) and are stored in its hardware, the card's read-only memory, or some other firmware mechanism. If assigned by the manufacturer, a MAC address usually encodes the manufacturer's registered identification number and may be referred to as the burned-in address. It may also be known as an Ethernet hardware address (EHA), hardware address or physical address. A network node may have multiple NICs and will then have one unique MAC address per NIC.

Now the question is How can we find the Mac Address using C language....
There may be many methods to do that but  i am using the sytem() function which is used to run the DOS command using C and the FILE handling to get the MAC address or pghysical address of the pc...

As we know we can get the physical or MAC address using the DOS command "ipconfig/all" ..u can get the mac address using this command from the dos prompt as shown in the figure below..
As you can se in the figure the Physical Address is 00-1C-C0-1A-6E-C0
Now mout ask is only to show this address as theoutput of our C program....
What firstly Strike in the mind,the idea is :
"To get the ipconfig/all command executed using sytem funtion and redirecting the output to the string or taking the output to the string and then getting the substring form that main string starting from the Physical Address that will be easy and simple....."
I also look for that and tried but was not able to do the same....any help in this regard will be appriciated
now what i have done to get the only MAC address or the physical addres,i just rediredced the output of the system funtion() with the inconfig/all command to a file ,read that file back line by line which is very necessary then search for the substring starting with Physical Address or containg the Physical Address,get that printed and get out of the loop using break....
You can see the code below...
#include <stdio.h>
#include<conio.h>
int main ()
{
  FILE *fp;
  clrscr();
  system ("ipconfig/all>f:\macid.txt");
   fp=fopen("f:\macid.txt","r");
   if(fp!=NULL)
   {
   char line[128];
   while(fgets(line,sizeof line,fp)!=NULL)
   {
   char *nwln=strchr(line,'\n');
   char *ptr;
   if(nwln!=NULL)
   *nwln='\0';
   ptr=strstr(line,"Physical Address");
   if(ptr!=NULL)
   {
   printf("%s\n",ptr);
   break;
   }
  }
   }
  getch();
  return 0;
} 
NOTE:Run the program using DOS Prompt instead of turboc ide....
Your comments and query are invited .....

Dos Command in C

This example will show how to use the DOS command in C and rediret its output to a file..
#include <stdio.h>
#include<conio.h>
void main ()
{
  FILE *fp;
  clrscr();
  printf ("Checking if processor is available...");
  if (system(NULL))
  {
  puts ("Ok");
  }
  else
  {
  printf("Processor is not available");
  getch();
  exit (1);
  }
  system ("dir>c:\dirlist.txt");
  printf("Done!!File created");
getch();
}
output:will create the file with name dirlist in the "c:\" drive..........