You are on page 1of 5

//A file is a place on the disk to store large volume of related data.

//Basic modes - r-read, w-write, a-append


#include<stdio.h>
main()
{
FILE *f1;//FILE is a defined data type under stdio.h, It is a structure defined in I/O
library, f1-file pointer used as a commn. link b/w system & prog.
char c;
printf("\nData input\n");
f1=fopen("Input","w"); /*Open the file Input, Creates or opens a file*/
while((c=getchar())!=EOF) /*get a character from key board until cntrl+z(Windows),
cntrl+d(UNIX) is pressed*/
putc(c,f1); /*write a character to file named input*/
fclose(f1); /*close the file input*/
printf("\nData output\n");
f1=fopen("INPUT","r"); /*Reopen the file input*/
while((c=getc(f1))!=EOF)
putchar(c);
//printf("%c",c);
fclose(f1); //closes a file after its use
system("pause");
}
//getc,putc - simplest file I/O functions equivalent to fgetc and fputc

#include<stdio.h>
main()
{
FILE *f1;
char c;
/*printf("\nData input\n");
f1=fopen("Input","a");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf("\nData output\n");
f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1); */

printf("\nData input\n");
f1=fopen("Input","a");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf("\nData output\n");
f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1);
system("pause");
}

//files handling integer values


#include<stdio.h>
main()
{
FILE *f1,*f2,*f3;
int num,i;
printf("enter the data");
f1=fopen("DATA","w");
for(i=1;i<=10;i++)
{
scanf("%d",&num);
putw(num,f1);//writes the integer to a file
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("EVEN","w");
f3=fopen("ODD","w");

while((num=getw(f1))!=EOF)//getw reads integer from a file


{
if(num%2==0)
putw(num,f2);
else
putw(num,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);

f2=fopen("EVEN","r");
f3=fopen("ODD","r");

printf("\nContents of f2\n");
while((num=getw(f2))!=EOF)
printf("\n%d",num);
printf("\nContents of f3\n");
while((num=getw(f3))!=EOF)
printf("\n%d",num);

fclose(f2);
fclose(f3);
system("pause");
}

/* A pointer variable holds the address of another variable


An ordinary variable stores data values*/

main()
{
int x=10,y=5,*p1=&x,*p2=&y;
float res;
//p1=&x;
//p2=&y;
res=(float)*p1/ *p2;
printf("Address of x=%u\n",&x);
printf("Address of y=%u\n",&y);
printf("Address of p1=%u\n",&p1);
printf("Address of p2=%u\n",&p2);
printf("Address of res=%u\n",&res);
printf("value of x =%d\n",x);
printf("value of y =%d\n",y);
printf("value of p1 =%d\n",*p1);
printf("value of p2 =%d\n",*p2);
printf("value of res =%f\n",res);
system("pause");
}
//Swap using pointers
main()
{
int temp,*a,*b,x,y;
scanf("%d%d",&x,&y);
a=&x;
b=&y;
temp=*a;
*a=*b;
*b=temp;
printf("*a=%d\t*b=%d\n",*a,*b);
printf("x=%d\ty=%d",x,y);
system("pause");
}

//Addition using pointers


main()
{
/* int *p is a integer pointer variable*/
int a,b,*p=&b,c;
//scanf("%d%d",&a,&b);
scanf("%d%d",&a,p);
c=a+*p;
printf("%d",c);
system("pause");
}

//Functions using pointers


void swap(int *, int *);
main()
{
int x,y;
scanf("%d%d",&x,&y);
printf("Values before swapping x=%d\ty=%d",x,y);
swap(&x,&y);//call by reference
printf("\nValues after swapping x=%d\ty=%d",x,y);
system("pause");
}
void swap(int *p,int *q)//address of x&y is assigned to pointer p&q
{*p=100;
*q=200;
int temp;
temp=*p;//values of x&y are dreferenced
*p=*q;
*q=temp;
return;
}

//Function returning a pointer


int * pointmax(int *,int *);
int main()
{
int a,b,*p;
printf("enter the values of a&b\n");
scanf("%d%d",&a,&b);
p=pointmax(&a,&b);
printf("The largest value is %d,%d,%d",*p,p,&b);
system("pause");
}
int * pointmax(int *x,int *y)
{
if (*x > *y)
return x;
else
return y;
}

//Functions returning multiple values


float compute(float, float *);
int main()
{
float r, area,circum;
printf("Enter the radius\n");
scanf("%f",&r);
area=compute(r,&circum);
printf("Area=%f\n",area);
printf("Circumference=%f\n",circum);
system("pause");
}

float compute(float r, float *p)


{
float a;
a=3.14*r*r;
*p=2.0*3.14*r;
return a;
}

You might also like