fscanf fails on an int

This is a discussion on fscanf fails on an int within the C Programming forums, part of the General Programming Boards category; This is my first post on cprogramming.com, so I apologize if this is in the wrong section or the answer ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Question fscanf fails on an int

    This is my first post on cprogramming.com, so I apologize if this is in the wrong section or the answer is easily found elsewhere.

    I'm working on a program that will read in student grades from a file and echo them back out to another file with an average semester grade and letter grade.

    My problem is that when my code attempts to read in the second student's grades, the program fails for no apparent reason.

    here is the code giving me the problem:
    Code:
    ...
            int n = 0, i = 0, a = 0, b = 0;
    	fscanf(read, "%d,%d\n", max_numb_students, numb_grades);
    
    	n = *max_numb_students;
    
    	i = *numb_grades;
    
    	*student_list = (struct student*) malloc(n*sizeof(struct student*));
    	
    	
    	for (n=0;n<*max_numb_students;n++){
    	
    		student_list[n]->lastname = fget_string(read, 1);
    		printf("%s,", student_list[n]->lastname);
    
    		student_list[n]->firstname = fget_string(read, 2);
    		printf("%s ", student_list[n]->firstname);
    		
    		fscanf(read, "%d", &b);
    		student_list[n]->id = b;
    		printf("%d ", student_list[n]->id);
    		
    		student_list[n]->grades = (int *) malloc(i*sizeof(int *));
    	
    		for (a=0;a<i;a++){
    
    			fscanf(read, "%d ", &b);
    			student_list[n]->grades[a] = b;
    			printf("%d ", student_list[n]->grades[a]);
    			
    		}
    ...
    My program seems to run through the first for loop once without a problem, but on the second run through, my program fails when it reaches the fscanf function in the second for loop.

    I would appreciate ANY help, as I'm a beginner.
    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose the stars on max_num_students and num_grades, everywhere you have them ... where you are using these variables you need integers, not pointers.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Hard to say exactly without context, and some helper functions, but one thing I noticed is that you're mallocing only enough space for max_numb_students pointers to student structs, not student structs. Also, you shouldn't cast malloc. The safest way to allocate something is:
    Code:
    thing *t;
    t = malloc(n * sizeof(*t));
    Dereference the place you're storing it and you'll always get the right type and allocation size. Since you're storing it in *student_list, you should do sizeof(**student_list):
    Code:
    *student_list = malloc(n * sizeof(**student_list));
    You have a similar problem with your grades list. Don't cast it, and do sizeof(*(student_list[n]->grades)).

    This could easily cause problems when you scan into one of those spots. You're probably already off the end of the memory you (incorrectly) allocated.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thanks for the replies!

    Long story short, your replies started a chain reaction that let me realize what i did wrong.
    And it made me make my code look better and less jumbled.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Issue?
    By matthewlane in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 04:55 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM

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