The need:
This is a program to group a given number of players according to their teams. This is same as program59 of this blog. The only difference is: here input has been taken from the file and the concerned file name has been taken through command line arguments.
The code:
--------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct record
{
char name[20],team[10];
float av;
}pl[15];
int main(int argc,char *argv[])
{
int i=0,j=0,k,count;
char x[10],filename[20];
FILE *fp;
printf("total number of arguments given=%d\n",argc);
printf("Executing program name is %s\n",argv[0]);
strcpy(filename,argv[1]);
if((fp = fopen(filename, "r"))==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s",&pl[i].name);
fscanf(fp,"%s",pl[i].team);
fscanf(fp,"%f",&pl[i].av);
i++;
}
count=i-1;
for(i=0;i<count;i++)
{
if(strcmp(pl[i].team,"\0")!=0)
{
strcpy(x,pl[i].team);
printf("\nplayers of team %s are =>\n",x);
for(j=0;j<count;j++)
{
if(strcmp(x,pl[j].team)==0)
{
printf("name => %s\n",pl[j].name);
printf("Average => %f\n",pl[j].av);
for(k=0;k<10;k++)
pl[j].team[k]='\0';
}
}
}
}
fclose(fp);
return 0;
}
--------------------------------------------
The approach: This program takes input from a file whose name is given through command line arguments. To know how command line arguments are used visit previous post. For other logic see program59. command line to run this program will be:
prog1.exe data.txt
where data.txt can be downloaded from this link. In menu bar there select file-> download as -> text.
This is a program to group a given number of players according to their teams. This is same as program59 of this blog. The only difference is: here input has been taken from the file and the concerned file name has been taken through command line arguments.
The code:
--------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct record
{
char name[20],team[10];
float av;
}pl[15];
int main(int argc,char *argv[])
{
int i=0,j=0,k,count;
char x[10],filename[20];
FILE *fp;
printf("total number of arguments given=%d\n",argc);
printf("Executing program name is %s\n",argv[0]);
strcpy(filename,argv[1]);
if((fp = fopen(filename, "r"))==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s",&pl[i].name);
fscanf(fp,"%s",pl[i].team);
fscanf(fp,"%f",&pl[i].av);
i++;
}
count=i-1;
for(i=0;i<count;i++)
{
if(strcmp(pl[i].team,"\0")!=0)
{
strcpy(x,pl[i].team);
printf("\nplayers of team %s are =>\n",x);
for(j=0;j<count;j++)
{
if(strcmp(x,pl[j].team)==0)
{
printf("name => %s\n",pl[j].name);
printf("Average => %f\n",pl[j].av);
for(k=0;k<10;k++)
pl[j].team[k]='\0';
}
}
}
}
fclose(fp);
return 0;
}
--------------------------------------------
The approach: This program takes input from a file whose name is given through command line arguments. To know how command line arguments are used visit previous post. For other logic see program59. command line to run this program will be:
prog1.exe data.txt
where data.txt can be downloaded from this link. In menu bar there select file-> download as -> text.
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply