/* DSA 1st programm
Write a program for copying content of one file to another in C
*/
#include<stdio.h>
#include<string.h>
struct emp
{
int id;
char name[30],address[30];
}b[4];
int main()
{
int temp_id=0;
char temp_name[30],temp_address[30];
int i,j;
FILE *fp,*fs;
fp=fopen(“c:/unsorted.txt”,”w”);
fs=fopen(“c:/sorted.txt”,”w”);
if(fp==NULL)
{
puts(“Cannot open the file unsorted.txt …….!!”);
}
if(fs==NULL)
{
puts(“Cannot open the file sorted.txt…….!!”);
}
for(i=0;i<=3;i++)
{
printf(“\nEnter your id>>”);
scanf(“%d”,&b[i].id);
printf(“\nEnter your name>>”);
scanf(“%s”,b[i].name);
printf(“\nEnter your address>>”);
scanf(“%s”,b[i].address);
fprintf(fp,”%d %s %s\n”,b[i].id,b[i].name,b[i].address);
}
temp_id=b[0].id;
strcpy(temp_name,b[0].name);
strcpy(temp_address,b[0].address);
for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
if(b[i].id>b[j].id)
{
temp_id= b[i].id;
b[i].id= b[j].id;
b[j].id= temp_id;
strcpy(temp_name,b[i].name);
strcpy(b[i].name,b[j].name);
strcpy(b[j].name,temp_name);
strcpy(temp_address,b[i].address);
strcpy(b[i].address,b[j].address);
strcpy(b[j].address,temp_address);
}
}
}
for(i=0;i<=3;i++)
{
fprintf(fs,”%d %s %s\n”,b[i].id,b[i].name,b[i].address);
}
fclose(fp);
fclose(fs);
return 0;
}
/*
Output:-
*****************************************UNSORTED.TXT***************************
3 a aa
2 b bb
1 a aa
0 o oo
*****************************************SORTED.TXT*****************************
0 o oo
1 a aa
2 b bb
3 a aa
********************************************************************************
The location of Output will be in C drive……..
*/
/*
Compiled and tested in Dev-C++ a GCC compiler
*/