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

You may need to set the buffer ptr to null:

memset(ptr, 0, sizeof(your structure));

Each student will typically be taking several courses each semester, so adding more classes into your student struct, would not be unreasonable.

Two other alternatives:

1) Add a classes struct, and nest it inside your student struct.

The one I like best is an array of char, where each class is listed by it's abbreviation (standardized), and it's course number: so BIO110 refers to Biology 110, PHY212 to Physics 212, etc.

Now every class has exactly 6 char's (maybe you want 7 or 8 for some schools), and by simple division, you easily have the number of classes, and finding any class within this string for a database, would be easy.

I believe you're working toward this, are you not?

commented: Adak, youve gave me a great idea m8! Thank you +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.