I have a file which looks like following:
ATOM HIS
ATOM TRP
ATOM PHE
I want to print the first column, following is my C-code:
#include<stdio.h>
#includ<stdlib.h>
void main
{
FILE *fp;
fp=fopen("xyz","r");
char *atm,*res;
char buff[200];
while(fgets(buff,sizeof (buff),fp)!=NULL){
i++;
}
rewind(fp);
atm=(char*)malloc(i * sizeof (char*));
res=(char*)malloc(i * sizeof (char*));
while(fgets(buff,sizeof (buff),fp)!=NULL){
fscanf(fp,"%s %s",&atm[i],&res[i]);
i++;
}
for(j=0;j<i;j++){
printf("%s\n",atm);
}
I would expect the following output:
ATOM
ATOM
ATOM
But it doesn't compile and says that:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
hence in printf
statement I have added &
to atm
(i.e &atm instead of atm). In this case the code compiles well but gives the following output:
AAAAAAAAAAAAAAAATOM
AAAAAAAAAATOM
AAAAAAAATOM
I will appreciate any suggestion regarding this.