This is the code
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
struct module
{
char *m_module[MAX];
// int m_status[MAX];
};
int main()
{
FILE *fp;
struct module m;
int i;
/*
if((fp = fopen("module_info.txt","wb")) == NULL)
{
printf("\nUnable to open the file!!!\n");
return 0;
}
for(i = 0 ; i < MAX ; i++)
{
printf("\nEnter the module no %d:",i+1);
m.m_module[i] = (char *)malloc(sizeof(char)*10);
__fpurge(stdin);
gets(m.m_module[i]);
// m.m_status[i] = 0;
}
fwrite(&m,sizeof(struct module),1,fp);
if(fclose(fp) == EOF)
{
printf("\nError in closing the file!!!");
exit(1);
}
*/
if((fp = fopen("module_info.txt","rb")) == NULL)
{
printf("\nUnable to open the file!!!\n");
return 0;
}
fread(&m,sizeof(struct module),1,fp);
for(i = 0 ; i < MAX ; i++)
{
printf("%s \n",m.m_module[i]);
}
if(fclose(fp) == EOF)
{
printf("\nError in closing the file!!!");
exit(1);
}
return 0;
}
getting segmentation fault in printf("%s \n",m.m_module[i]) statement.
what is wrong?