Hello
So I have a simple structure :
struct students
{
char name[30];
unsigned int noofcourses;
char course[50];
char status[30];
}arr[50]; //50 students
I would like to know is how could I read multiple courses in per student from memory.
Example:
Joey Brown
4
Math$Chem$Phy$Cooking
Enrolled m8!!
So Im doing it like this and also writing it to .dat file
//ptr points to the memory block which holds the data
for(i=MIN;;i++)
{
sscanf(ptr,"%30[^\n]%n",arr[i].name,&n);
fwrite(arr[i].name,1,n,datptr);
//printf("%s",arr[i].name);
ptr+=n;//skip n one-byte chars
++ptr;//skip linefeed
sscanf(ptr,"%d%n",&arr[i].noofcourses,&n);
fwrite(&arr[i].noofcourses,4,n,datptr);
//printf("\n%d",arr[i].noofcourses);
ptr+=n;
++ptr;
for(j=MIN;j<arr[i].noofcourses;j++)
{
sscanf(ptr,"%50[^$],%n",arr[i].course[j],&n); //courses are delimited with '$'
fwrite(arr[i].course[j],1,n,datptr);
printf("\n%s",arr[i].course[j]);
ptr+=n;
++ptr;
if(*(ptr)=='\n')
{
break;
}
}
sscanf(ptr,"%30[^\n]%n",arr[i].status,&n);
fwrite(arr[i].status,1,n,datptr);
//printf("\n%s\n\n",arr[i].status);
ptr+=n;
if(*(ptr)!='\n')
{
break;
}
while(*(ptr)=='\n')
{
ptr+=1;
}
}
It does write up in mybin.dat but it also pulls alot of junk with it, and when I try to read it it prints out lovecraftian horror :).
Thanks for any input, if any.
Joey