CAssign 9
CAssign 9
CAssign 9
if(sptr==NULL || dptr==NULL)
{
printf("file does not exist");
exit(0);
}
while((ch=fgetc(sptr))!=EOF)
{
fputc(ch,dptr);
}
printf("file copied");
fclose(sptr);
fclose(dptr);
return 0;
}
CODE 1:
OUTPUT 1:
Inference :
1)”w” creates a file for writing .If the file already exist , it
discards the contents.
2) file opening mode : ”r+”
sptr=fopen("source.txt","r+");
dptr=fopen("destination.txt","r+");
output:
file does not exist
inference: Update the records in a file by overwriting the
record. If file does not exists , it does not create the file .
3) file opening mode : ”w+”
sptr=fopen("source.txt","r");
dptr=fopen("destination.txt","w+");
output:
file copied
inference : Create a file for update .If the file already exist it
discards the contents.
4) file opening mode : ”a”/”a+”
sptr=fopen("source.txt","r");
dptr=fopen("destination.txt","a");
OUTPUT
#include <stdio.h>
#include <string.h>
void main()
{
FILE *sptr,*dptr;
char str[10][50], temp[50];
int no,i,j;
sptr=fopen("source.dat","a");
dptr=fopen("destination.dat","a");
fclose(sptr);
fclose(dptr);
}
CODE:
OUTPUT:
4 . command line arguments
Source code:
#include <stdio.h>
#include <stdlib.h>
{
int sum=0,counter,numbers[10];
for(counter=0;counter<argc;counter++)
{
printf("\nargv[%d]=%s",counter,argv[counter]);
numbers[counter] =atoi(argv[counter]);
sum+=numbers[counter];
}
printf("\nsum of numbers:%d",sum);
}}
CODE:
OUTPUT: