Thread: Problem with passing structure array to function prototype

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    12

    Question Problem with passing structure array to function prototype

    Sorry to bother you all again. You all seem like experts next to me, so I thought I'd pick your brains. I'm fairly clueless when it comes to pointers, and I'm sure I've made quite a few rookie mistakes. I'm facing 3 problems with this code:

    1. First and foremost, it doesn't appear that I'm correctly passing or referencing the pointers. The read_student printf is returning gibberish, and the main printf is returning even worse gibberish.

    2. The call to read_student only seems to work on the first loop. After that, neither of the printfs in read_student print on each loop.

    3. The read_student printf counter is initially printing 50 times, yet there is only 25 lines of text to read. There's only a single '\n' at the end of each line. This suggests that the fgets is reading 50 lines, but I don't see how.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define STUDENT_FILE "student.txt"
    
    FILE *textin;
    
    int highestMark, counter, lineCounter, nullCounter, i, j;
    char line[64];
    
    struct student {
    	char id[9];
    	int paperOne;
    	int paperTwo;
    	int paperThree;
    	int paperFour;
    	int paperFive;
    	int paperSix;
    	int paperSeven;
    	int paperEight;
    };
    
    void read_student(struct student *);
    void print_student(struct student *);
    void copy(int *x, char *y, char *z);
    
    int main() {
    	textin = fopen(STUDENT_FILE, "r");
    	struct student grades[25];
    	lineCounter = 0;
    	for (i=0; i<25; i++) {
    		read_student(&grades[i]);
    	/*	printf("%s\n", grades[i].id);
    		print_student(*pGrades[i]); */
    		printf("Main counter: %i\n", lineCounter);  /* for testing */
    		printf("Printf main: %s\n", grades[i].id);	/* for testing */
    		lineCounter++;
    		rewind(textin);
    	}
    	fclose(textin);
    }
    
    void read_student(struct student *grades) {
    	counter = 0;
    	while (fgets(line, 65, textin) != NULL) {
    		if (counter == lineCounter) {
    			sscanf(line, "%s %d %d %d %d %d %d %d %d", grades->id, &grades->paperOne, &grades->paperTwo, 
    			&grades->paperThree, &grades->paperFour, &grades->paperFive, &grades->paperSix, &grades->paperSeven, 
    			&grades->paperEight);
    		}
    		if (line[0] != '\n') {
    			counter++;
    		}
    	}
    }
    EDIT: Solved problem 2 by placing a rewind(textin) in the main loop.

    EDIT2: Solved. I had to add a check for the return character '\n' on its own line. fgets was reading end of line at \r, then promptly reading end of line again at the \n. I've uploaded the fix and the current build in case anyone else gets stuck in a similar situation. Thanks to everyone who lent a hand.
    Last edited by Gareth321; 05-29-2011 at 11:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-15-2010, 09:14 AM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. function prototype with array
    By melodia in forum C Programming
    Replies: 3
    Last Post: 11-15-2009, 02:31 PM
  4. passing array to function problem
    By thestien in forum C++ Programming
    Replies: 13
    Last Post: 05-10-2008, 01:45 AM
  5. Replies: 6
    Last Post: 02-15-2005, 11:20 PM