fwrite problems

This is a discussion on fwrite problems within the C Programming forums, part of the General Programming Boards category; I'm trying to write some student records to a binary file... Right when my program gets to part where it ...

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

    fwrite problems

    I'm trying to write some student records to a binary file... Right when my program gets to part where it goes to write my structure to a file I get a segment fault during runtime.

    here is the function:
    Code:
    /*Function for writing student information at beginning semester*/
    void student()
    {
    	int number, i,j;
    	printf("How many total students?\n",number);
    	scanf("%d",&number);
    	fgets(dummy,50,stdin);
    	struct rec r[number];
    	
    	for( i=0; i<number; i++)
    	{	
    		printf("Student number %d, enter name\n",i);
    		fflush(stdin);
    		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);
    	}
    	
    	
    
    }
    here is the entire program

    Code:
    #include <stdio.h>
    
    FILE *f;
    char dummy[50];
    /*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);
    	fgets(dummy,50,stdin);
    	struct rec r[number];
    	
    	for( i=0; i<number; i++)
    	{	
    		printf("Student number %d, enter name\n",i);
    		fflush(stdin);
    		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("%-11s %-30s %-6s %-4s %-3s %-10s %-20s\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
    	
    
    
    }
    Last edited by Newworld; 10-11-2004 at 11:29 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Um.... I take it you saw this in someone's sig?
    Code:
    while (1)
       		malloc (1 << 20);
      	return 0; // not reached
    	
    
    
    }
    Yeah... Take those first two lines out. (And thank you for fixing your code tags.)

    [edit]
    You should look at the FAQ as to why fflush( stdin ) is wrong.
    [/edit]

    Quzah.
    Last edited by quzah; 10-11-2004 at 11:31 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    printf("How many total students?\n",number);
    What is number doing in that printf() statement?

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    oh my.... please don't start pasting stuff in my sig into your programs... it's supposed to be a joke =/
    hello, internet!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Nooo - make it stop - the fflush(stdin) burns my eyes!

    > f = fopen ("record","rwtb");
    Trying to cover all the bases here?
    List all the possible letters and hope something will stick?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by Salem
    Nooo - make it stop - the fflush(stdin) burns my eyes!

    > f = fopen ("record","rwtb");
    Trying to cover all the bases here?
    List all the possible letters and hope something will stick?
    wouldnt that be fopen ("record", "rwba+"); tho?
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. Determining fwrite errors
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 05:30 AM
  5. Having Problems with fwrite();
    By Vash_Calamidade in forum C Programming
    Replies: 6
    Last Post: 02-04-2002, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21