Thread: student binary file program....

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    student binary file program....

    I'm writing a program to store information about students using structures. We store the information in a binary file. I am getting some compiler errors using the unix machines at school. I can't see the errors that it is giving me. Any help would be greatly appreciated.
    Here are the errors:
    project2.c:15: two or more data types in declaration of `student'
    project2.c: In function `student':
    project2.c:34: parse error before ';' token
    project2.c: In function `delete':
    project2.c:58: `rec' undeclared (first use in this function)
    project2.c:58: (Each undeclared identifier is reported only once
    project2.c:58: for each function it appears in.)
    project2.c: In function `print':
    project2.c:108: warning: assignment makes integer from pointer without a cast
    project2.c:109: request for member `print' in something not a structure or union
    project2.c:135:2: warning: no newline at end of file


    Here is my code:
    Code:
    #include <stdio.h>
    
    FILE *f;
    /*Struct rec will be the student format*/
    struct rec 
    {	
    	long id;
    	char name[30];
    	unsigned char age;
    	unsigned char t1, t2, t3;
    	unsigned char p1, p2, p3, p4, p5;
    	unsigned int sex:1;
    	unsigned int class:3;
    }
    /*Function for writing student information at beginning semester*/
    void student()
    {
    	int number, i,j;
    	printf("How many total students?\n",number);
    	struct rec r[number];
    	
    	for( i=0; i<number; i++)
    	{	
    		printf("Student number %d, enter name\n",i);
    		fgets(r[i].name,30,stdin);
    		fflush(stdin);
    		printf("Enter id\n");
    		scanf("%d\n",&j);
    		fflush(stdin);
    		r[i].id=j;
    		printf("Enter age\n");
    		scanf("%d\n",&j);
    		fflush(stdin);
    		r[i].age=j;
    		printf("Enter 0 for male, 1 for female\n");
    		scanf("%d\n",&j;
    		fflush(stdin);
    		r[i].sex=j;
    		printf("Enter 0 - Freshman, 1 - Sophmoore, 2 - Junior, 3 - Senior\n");
    		scanf("%d\n",&j);
    		fflush(stdin);
    		r[i].class=j;
    		fwrite(&r[i], sizeof(struct rec),1,f);
    	}
    	
    	
    
    }
    /*Function to delete a student from the list*/
    void delete()
    {
    	int ret, i,j,std;
    	/*use rec zero as a blank record to overwrite student*/
    	struct rec zero; 
    	printf("How many students to delete?\n");
    	scanf("%d\n",&i);
    	fflush(stdin);
    	for (j=0;j<i;j++)
    	{	printf("Enter student number\n");
    		scanf("%d\n",&std);
    		fflush(stdin);
    		ret= fseek(f,std*(sizeof(rec)),SEEK_SET);
    		fwrite(&zero,sizeof(struct rec),1,f);
    	}
    		
    }
    /*Function to input scores for students*/
    void score()
    {
    	int ret, i, j,std;
    	struct rec test[50];
    	printf("How many students?\n");
    	scanf("%d\n",i);
    	fflush(stdin);
    	for(j=0;j<i;j++)
    	{
    		ret= fseek(f,0,SEEK_SET);
    		fread(&test[i],sizeof(struct rec),1,f);
    	}
    	for(j=0;j<i;j++)
    	{	
    		printf("Enter student number\n");
    		scanf("%d\n",&std);
    		fflush(stdin);
    		fread(&test,sizeof(struct rec),1,f);
    		printf("Enter test scores, then project scores\n");
    		scanf("%d %d %d %d %d %d %d %d",&test[i].t1,&test[i].t2,&test[i].t3,&test[i].p1,&test[i].p2,&test[i].p3,&test[i].p4,&test[i].p5);
    		fflush(stdin);
    	}
    	for(j=0;j<i;j++)
    	{	
    		ret= fseek(f,0,SEEK_SET);
    		fwrite(&test[i],sizeof(struct rec),1,f);
    	}
    }
    /*Function used to print current class information*/
    void print()
    {
    	int ret, std, i;
    	char s;
    	struct rec print[50];
    	printf("%-11d %-32d %-7d %-5d %-5d %-13d %-22d\n","   ID","       Name","Class","Sex","Age"," T Scores","   P Scores");
    	printf("---------  ------------------------------  -----  ---  ---  -----------  --------------------  \n");
    	printf("Enter number of students\n");
    	scanf("%d\n",&std);
    	fflush(stdin);
    	ret= fseek(f,0,SEEK_SET);
    	for(i=0;i<std;i++)
    	{
    		fread(&print[i],sizeof(struct rec),1,f);
    	}
    	for(i=0;i<std;i++)
    	{
    		s = print[i].sex==0?"M":"F";
    		printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",print[i].id,print[i].name[30],print[i].class,s,print[i].age,print[i].t1.print[i].t2,print[i].t3,print[i].p1,print[i].p2,print[i].p3,print[i].p4,print[i].p5);
    	}
    }
    main()
    {
    	int choice;
    	f = fopen ("record","rwtb");
    	printf("Choose option by number,\n1 - Enter student information\n2 - Enter test or program scores\n3 - Print class report\n4 - Delete Students\n");
    	scanf("%d\n",&choice);
    	fflush(stdin);
    	switch(choice)
    	{	case 1: student();
    			break;
    		case 2: score();
    			break;
    		case 3: print();
    			break;
    		case 4: delete();
    			break;
    	}
    	fclose(f);
    
    
    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
    scanf("%d\n",&j;
    always fix the parse errors first, then recompile and see what errors have gone away.

    not related specifically to your errors, but never the less true: never use fflush (stdin);, and main always returns an int; explicitly have it return one of 0, EXIT_SUCCESS, or EXIT_FAILURE
    hello, internet!

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    k, fixed the problems...
    recompiled and still getting:

    project2.c:17: two or more data types in declaration of `student'
    project2.c: In function `delete':
    project2.c:62: `rec' undeclared (first use in this function)
    project2.c:62: (Each undeclared identifier is reported only once
    project2.c:62: for each function it appears in.)
    project2.c: In function `print':
    project2.c:114: warning: assignment makes integer from pointer without a cast
    project2.c:115: request for member `print' in something not a structure or union
    project2.c:142:2: warning: no newline at end of file

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Newworld
    k, fixed the problems...
    recompiled and still getting:

    project2.c:17: two or more data types in declaration of `student'
    project2.c: In function `delete':
    project2.c:62: `rec' undeclared (first use in this function)
    project2.c:62: (Each undeclared identifier is reported only once
    project2.c:62: for each function it appears in.)
    project2.c: In function `print':
    project2.c:114: warning: assignment makes integer from pointer without a cast
    project2.c:115: request for member `print' in something not a structure or union
    project2.c:142:2: warning: no newline at end of file

    When it says something is wrong with line 17 but line 17 looks OK, look up (that is, what happened before line 17):

    Code:
    struct rec 
    {	
    	long id;
    	char name[30];
    	unsigned char age;
    	unsigned char t1, t2, t3;
    	unsigned char p1, p2, p3, p4, p5;
    	unsigned int sex:1;
    	unsigned int class:3;
    }   /* <--- Missing semicolon */
    /*Function for writing student information at beginning semester*/
    void student()

    Regards,

    Dave

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    ahhh yes, i completely forgot about needing a ; after ) when declaring a structure. Thank you.

    It seems I have just two last errors:

    project2.c: In function `print':
    project2.c:114: warning: assignment makes integer from pointer without a cast
    project2.c:115: request for member `print' in something not a structure or union

    I believe it has to do with the line:

    Code:
     s = print[i].sex==0?"M":"F";
    Since I have to save space by making print[i].sex a 1 bit unsigned int, I then needed to convert in to either "M" or "F" for printing to the screen.

    For the second error, I believe it is for the long print line of:
    Code:
     printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",print[i].id,print[i].name[30],print[i].class,s,print[i].age,print[i].t1.print[i].t2,print[i].t3,print[i].p1,print[i].p2,print[i].p3,print[i].p4,print[i].p5);
    I am trying to print out to the screen my struct print[]

    Thanks for any help

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Newworld
    ahhh yes, i completely forgot about needing a ; after ) when declaring a structure. Thank you.

    It seems I have just two last errors:

    project2.c: In function `print':
    project2.c:114: warning: assignment makes integer from pointer without a cast
    project2.c:115: request for member `print' in something not a structure or union

    I believe it has to do with the line:

    Code:
     s = print[i].sex==0?"M":"F";
    Since I have to save space by making print[i].sex a 1 bit unsigned int, I then needed to convert in to either "M" or "F" for printing to the screen.

    For the second error, I believe it is for the long print line of:
    Code:
     printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",print[i].id,print[i].name[30],print[i].class,s,print[i].age,print[i].t1.print[i].t2,print[i].t3,print[i].p1,print[i].p2,print[i].p3,print[i].p4,print[i].p5);
    I am trying to print out to the screen my struct print[]

    Thanks for any help
    You have given the format specifier for printing sex to be %c ( a char), so:
    Code:
    s = print[i].sex==0?'M':'F';
    Actually you have lots of errors in your format statements. Once you get it to compile, you can probably fix the output to suit.

    Look carefully at the print statement that it flagged. If necessary, break up the statement over many lines so that you can see where the error is.

    Code:
     printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",
      print[i].id,
      print[i].name[30],
      print[i].class,
      s,
      print[i].age,
      print[i].t1.
      print[i].t2,
      print[i].t3,
      print[i].p1,
      print[i].p2,
      print[i].p3,
      print[i].p4,
      print[i].p5);
    If you don't see it yet, let the compiler tell you.

    Good Luck! (Still lots of work to do)

    Regards,

    Dave
    Last edited by Dave Evans; 10-10-2004 at 03:47 PM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    hmm.. I finally got it to compile, but it looks like I'm not correct in my coding for writing my structure to the binary file:

    the program works fine as I go to the student function to enter info, I get down to where I enter the class of the student and then after I enter it I get Segment Fault and the program closes.

    I checked my textbook and It looks like I'm doing everything correct...

    here is the updated code:
    Code:
    #include <stdio.h>
    
    FILE *f;
    
    /*Struct rec will be the student format*/
    struct rec 
    {	
    	long id;
    	char name[30];
    	unsigned char age;
    	unsigned char t1, t2, t3;
    	unsigned char p1, p2, p3, p4, p5;
    	unsigned int sex:1;
    	unsigned int class:3;
    };
    /*Function for writing student information at beginning semester*/
    void student()
    {
    	int number, i,j;
    	printf("How many total students?\n",number);
    	scanf("%d",&number);
    	fflush(stdin);
    	struct rec r[number];
    	
    	for( i=0; i<number; i++)
    	{	
    		printf("Student number %d, enter name\n",i);
    		fgets(r[i].name,30,stdin);
    		fflush(stdin);
    		printf("Enter id\n");
    		scanf("%d",&j);
    		fflush(stdin);
    		r[i].id=j;
    		printf("Enter age\n");
    		scanf("%d",&j);
    		fflush(stdin);
    		r[i].age=j;
    		printf("Enter 0 for male, 1 for female\n");
    		scanf("%d",&j);
    		fflush(stdin);
    		r[i].sex=j;
    		printf("Enter 0 - Freshman, 1 - Sophmoore, 2 - Junior, 3 - Senior\n");
    		scanf("%d",&j);
    		fflush(stdin);
    		r[i].class=j;
    		fwrite(&r[i], sizeof(struct rec),1,f);
    	}
    	
    	
    
    }
    /*Function to delete a student from the list*/
    void delete()
    {
    	int ret, i,j,std;
    	/*use rec zero as a blank record to overwrite student*/
    	struct rec zero; 
    	printf("How many students to delete?\n");
    	scanf("%d",&i);
    	fflush(stdin);
    	for (j=0;j<i;j++)
    	{	printf("Enter student number\n");
    		scanf("%d",&std);
    		fflush(stdin);
    		ret= fseek(f,std*(sizeof(struct rec)),SEEK_SET);
    		fwrite(&zero,sizeof(struct rec),1,f);
    	}
    		
    }
    /*Function to input scores for students*/
    void score()
    {
    	int ret, i, j,std;
    	struct rec test[50];
    	printf("How many students?\n");
    	scanf("%d",i);
    	fflush(stdin);
    	for(j=0;j<i;j++)
    	{
    		ret= fseek(f,0,SEEK_SET);
    		fread(&test[i],sizeof(struct rec),1,f);
    	}
    	for(j=0;j<i;j++)
    	{	
    		printf("Enter student number\n");
    		scanf("%d",&std);
    		fflush(stdin);
    		fread(&test,sizeof(struct rec),1,f);
    		printf("Enter test scores, then project scores\n");
    		scanf("%d %d %d %d %d %d %d %d",&test[i].t1,&test[i].t2,&test[i].t3,&test[i].p1,&test[i].p2,&test[i].p3,&test[i].p4,&test[i].p5);
    		fflush(stdin);
    	}
    	for(j=0;j<i;j++)
    	{	
    		ret= fseek(f,0,SEEK_SET);
    		fwrite(&test[i],sizeof(struct rec),1,f);
    	}
    }
    /*Function used to print current class information*/
    void print()
    {
    	int ret, std, i;
    	char s;
    	struct rec print[50];
    	printf("%-11d %-32d %-7d %-5d %-5d %-13d %-22d\n","   ID","       Name","Class","Sex","Age"," T Scores","   P Scores");
    	printf("---------  ------------------------------  -----  ---  ---  -----------  --------------------  \n");
    	printf("Enter number of students\n");
    	scanf("%d",&std);
    	fflush(stdin);
    	ret= fseek(f,0,SEEK_SET);
    	for(i=0;i<std;i++)
    	{
    		fread(&print[i],sizeof(struct rec),1,f);
    	}
    	for(i=0;i<std;i++)
    	{
    		s = print[i].sex==0?'M':'F';
    		printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",print[i].id,print[i].name[30],print[i].class,s,print[i].age,print[i].t1,print[i].t2,print[i].t3,print[i].p1,print[i].p2,print[i].p3,print[i].p4,print[i].p5);
    	}
    }
    int main(void)
    {
    	int choice;
    	f = fopen ("record","rwtb");
    	printf("Choose option by number,\n1 - Enter student information\n2 - Enter test or program scores\n3 - Print class report\n4 - Delete Students\n");
    	scanf("%d",&choice);
    	fflush(stdin);
    	switch(choice)
    	{	case 1: student();
    			break;
    		case 2: score();
    			break;
    		case 3: print();
    			break;
    		case 4: delete();
    			break;
    	}
    	fclose(f);
    	while (1)
       		malloc (1 << 20);
      	return 0; // not reached
    	
    
    
    }

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I checked my textbook and It looks like I'm doing everything correct...
    Well, on to the next step. If your program bombs out after a particular point, put printf() statements to see how far it really gets:

    Code:
    		printf("Enter 0 - Freshman, 1 - Sophmoore, 2 - Junior, 3 - Senior\n");
    		scanf("%d",&j);
                    printf("Debug: j = %d\n", j);
                    printf("Debug: i = %d\n", i);
    		fflush(stdin);
    		r[ i ].class=j;
                    printf("Debug: Getting ready to write r[%d]\n", i);
    		fwrite(&r[ i ], sizeof(struct rec),1,f);
    Just keep putting in those printf() until you get a clue.

    Now, by the way, did you ever check to see if the file was opened properly? You should always do this.

    Regards,

    Dave
    Last edited by Dave Evans; 10-10-2004 at 04:49 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    hmm, ok I've tried doing the printf debugging and it seems that I have some major problem when I'm trying to enter the name using

    Code:
    fgets(r[i].name,30,stdin);
    when entering the info it does prints "Enter name" and then goes straight to Enter ID... it doesn't give the user a chance to enter a name

    when I use printf t check name its just all empty spaces...


    Also you said to check if the file has been opened correctly... How would I check to make sure if the file has been opened?

    Thanks for the help!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've only read your last post, but I suspect that you're mixing input types, which will give you this sort of problem. I'm guessing that you've just got done using scanf before your fgets call. scanf leaves the newline in the input stream, and since it's there, and fgets stops reading when it gets a newline... Well you can see the problem.

    ...checks code posted before your last post...

    Yup. I was right. While you're fixing that, jump over and read this FAQ entry to see why this isn't doing what you think it should.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM
  2. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM