I'm Using fwrite
to print the contents of a sturcture in a file.
#include <stdio.h>
#include <stdlib.h>
struct com{
int code;
char* name;
};
int main()
{
struct com py[2]={{21,"Monty Python"},
{22,"Python lang"}
};
FILE * fp;
if((fp=fopen("data.txt","w+"))==NULL)
exit(1);
fwrite(&py,sizeof(struct com),1,fp);
/*int i;
for(i=0;i<2;i++)
fprintf(fp,"code:%d,name:%s\n",py[i].code,py[i].name);
*/
fclose(fp);
return 0;
}
It either prints nothing or prints some garbage.
While the second method using fprintf()
seems to work.