CAssign 9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Programming Assignment 9

NAME: KEERTHANA SENTHILNATHAN


ROLL NO.: 108120050
BRANCH : ECE DEPT 1ST YEAR
DATE:3.03.2021
SECTION: B
1.Copy source file to destination file
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *sptr,*dptr;
sptr=fopen("source.txt","r");
dptr=fopen("destination.txt","w");

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

inference : append , open or create a file to write at the end . If


the file exists Previous contents are not discarded
2.Transcation program
OUTPUT
3.read strings from user and store in source file
.sort the strings in alphabetical order and write the
sorted strings in destination file.

#include <stdio.h>
#include <string.h>

void main()
{
FILE *sptr,*dptr;
char str[10][50], temp[50];
int no,i,j;

printf("Enter number of strings: ");


scanf("%d",&no);

sptr=fopen("source.dat","a");
dptr=fopen("destination.dat","a");

for ( i = 0; i <=no; i++)


{
fgets(str[i], sizeof(str[i]), stdin);
fprintf(sptr,"%s",str[i]);
}

for ( i = 0; i <=no; i++)


{
for ( j = i + 1; j <=no; j++)
{

if (strcmp(str[i], str[j]) > 0)


{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}

for (i = 0; i <=no; i++)


{
fprintf(dptr,"%s",str[i]);

fclose(sptr);
fclose(dptr);
}

CODE:

OUTPUT:
4 . command line arguments
Source code:
#include <stdio.h>
#include <stdlib.h>

void main(int argc,char* argv[])

{
int sum=0,counter,numbers[10];

printf("name of the program:%s",argv[0]);


if(argc==1)
printf("name of the program is only the argument");
if(argc>=2)
{
printf("\nnumber of arguments passed:%d",argc);
printf("\nfollowing are the arguments passed\n");

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:

You might also like