c program for file code ,fputs(),fflush,strcpy,strcmp using for loop
#include<stdio.h>
void main()
{
FILE *f,*q;
char any;
char nm[20],t[20];
char m[20][20];
int i,j,n;
clrscr();
f=fopen("e:\names.txt","w");
do
{
fflush(stdin);
printf("\n Enter a name");
gets(nm);
fputs(nm,f);
fputs("\n",f);
fflush(stdin);
any=getchar();
}while(any=='y');
fclose(f);
f=fopen("e:\names.txt","r");
q=fopen("e:\sorted.txt","w");
n=0;
while(!feof(f))
{
fgets(nm,20,f);
if(feof(f))
break;
strcpy(m[n],nm);
n++;
}
fclose(f);
printf("\n list of students");
for(j=0;j<1;j++)
puts(m[j]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(m[i],m[j]>0) )
{
strcpy(t,m[i]);
strcpy(m[i],m[j]);
strcpy(m[j],t);
}
}
}
printf("\n sorted alphabetically...\n");
for(j=0;j<n;j++)
{
puts(m[j]);
fputs(m[j],q);
}
fclose(q);
getch();
}
#include<stdio.h>
void main()
{
FILE *f,*q;
char any;
char nm[20],t[20];
char m[20][20];
int i,j,n;
clrscr();
f=fopen("e:\names.txt","w");
do
{
fflush(stdin);
printf("\n Enter a name");
gets(nm);
fputs(nm,f);
fputs("\n",f);
fflush(stdin);
any=getchar();
}while(any=='y');
fclose(f);
f=fopen("e:\names.txt","r");
q=fopen("e:\sorted.txt","w");
n=0;
while(!feof(f))
{
fgets(nm,20,f);
if(feof(f))
break;
strcpy(m[n],nm);
n++;
}
fclose(f);
printf("\n list of students");
for(j=0;j<1;j++)
puts(m[j]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(m[i],m[j]>0) )
{
strcpy(t,m[i]);
strcpy(m[i],m[j]);
strcpy(m[j],t);
}
}
}
printf("\n sorted alphabetically...\n");
for(j=0;j<n;j++)
{
puts(m[j]);
fputs(m[j],q);
}
fclose(q);
getch();
}
0 comments: