Thread: trouble reading from a file...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    trouble reading from a file...

    I've been trying to write this for quite a while, and still have a lot of problems. I know it is still a mess, can't figure out a few computations (real headache), but at the moment the worst problem is that my program does not open a file, or at least it seems to me so...
    Can someone take a look at it, and tell me what I did wrong that it doesn't open that file??? And if you have any hints about the computations, I'd be really grateful...
    The file contains a formatted input which is sorted alphabetically data about each course. The program is to look for all the courses a student took and compute his/her gpa...

    Code:
       
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    /* type definitions */
    struct StudentData {
    	int idnum;
    	char name[15];
    	char code[5];
    	int credits;
    	char grade[1];
    	int gradenum;
    	double gradecredit;
    };
    
    typedef struct StudentData DATASTU;
    
    
    
    
    int main( void )
    {
    int TotalSemHrCompl = 0;
    int i = 0;
    double Sem_gpa = 0.0;
    DATASTU students[1000] = {0}; 
    FILE *incoming;
    
    
    
    if( (incoming = fopen( "student.dat", "r" )) != NULL )
       {
          fscanf( incoming, "%d %s %s %d %s\n", &students->idnum, 
    &students->name, &students->code, &students->credits, &students->grade); 
          fclose( incoming );
       }
    else printf("Could not open the file\n");
    
    
    
    for(i=0; i<1000; i++)
    {
    switch(students[i].grade[1]) 
    {
    case 'A': 
    case 'a': students->gradenum = 4; break;
    case 'B': 
    case 'b': students->gradenum = 3; break;
    case 'C': 
    case 'c': students->gradenum = 2; break;
    case 'D': 
    case 'd': students->gradenum = 1;
    }
    }
    
    
    
    for ( i = 0; i < 1000; i++ )
    {
    	students[i].gradecredit = (students[i].grade[1]) * (students[i].credits);
    }
    
    
    
    for ( i = 0; students[i].idnum != 0; i++ )
    {
    	if ( students[i].idnum != students[++i].idnum )
    	{
    		printf( "Student Name: %s\n", students[--i].name);
    		printf( "Student ID number: %d\n\n", students[++i].idnum);
    		printf( "Course\t\tCourse\t\tCourse\n");
    		printf( " Code\t\tCredits\t\tGrade\n");
    	}
    /* trying to write code to print the credits, code and grade for 
    each course of a student... */
    
    
    	for ( i = 0; students[i].idnum = students[++i].idnum; students[i].idnum != students[++i].idnum, i++)
    	{
    		TotalSemHrCompl = students[i].credits + students[++i].credits;
    
    		printf( "Total semester Course Credits Completed: %d\n", TotalSemHrCompl );
    	}
    
    
    	for ( i = 0; students[i].idnum = students[++i].idnum; students[i].idnum != students[++i].idnum, i++)
    	{
    		Sem_gpa = (students[i].gradecredit + students[++i].gradecredit) /  TotalSemHrCompl;
    
    		printf( "Semester Grade Point Average: %.2f\n", Sem_gpa); 
    	}
    }
    
    
    
    
    
    
    
    
    return 0;
    }
    The student.dat file contains this:


    .
    12345678 Bokow, A. NE123 3 A
    12345678 Bokow, A. WE234 4 A
    12345678 Bokow, A. GH345 2 B
    34567890 Sandler, A. WE231 4 B
    19876543 Zigow, H. NE876 4 C
    19876543 Zigow, H. KJ456 4 A
    Thanks in advance for anything which would help

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208

    Re: trouble reading from a file...

    Originally posted by Nutka

    for ( i = 0;
    students[i].idnum =students[++i].idnum; students[i].idnum !=students[++i].idnum,
    i++)
    {
    I'm haven't looked over the rest of you code but there are a lot of problems in the these loops in the parentheses.

    i = 0; is ok
    the second part in the loop should be a boolean equation but you are doing a variable assignment, and even if that is what you want to do there this is still not right becuase the behavor of this is undefined ( look at past postings about the behavior of ++i and i++ )

    the third section of your loop evaluates a boolean equation ( students[i].idnum !=students[++i].idnum, ) but doesn;t do anything at all with the resulting value, so It cannot be what you meant to do

    and finally the i++ is ok

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    also are you aware of the fact that each iteration of your loop increments the value of i by 4 ?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Oh, thanks, I understand this \0 at the end of the string problem, I fixed it now.

    Second, your students have "lastname initial", and the space separation defeats "%s" conversions
    That's a surprise to me, I thought only \n can cause the problem... so I thought it's OK.


    also are you aware of the fact that each iteration of your loop increments the value of i by 4 ?
    I was trying to avoid it by incrementing and decrementing i constantly, but I only got caught in my own trap. How can I overcome this? How can I display a few records and then still increment num?
    Also, how can I find out how many courses each student took and thus how many times I should do the adding of grades etc?

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Ok, I'll do that, I always seem to be a little afraid to use functions for some reason but I'm going to implement them right now to make it all look neat.
    And I'll get to write more of the code to get the students courses ready to be display. Thanks a lot, Salem!

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    if you want to use the number i + 1 , but you don't want to change i , just write i + 1, not i++ or ++i

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    worked on it...

    Hi...
    I worked on the code, finished it. Unfortunately the program now stays in loop infinitely, and as far as I can notice, it doesn't display the data. I think I messed up with num and course, but can't figure out what... Could someone tell me what I did wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    /* type definitions */
    struct StudentData {
        int     idnum;  
        char    lname[16];
        char    fname[16];
        char    code[6];
        int     credits;
        char    grade[2];
        int     gradenum;
        double  gradecredit;
    };
    
    
    typedef struct StudentData DATASTU;
    
    /* function prototypes */
    void CalcGradeCredit ( struct StudentData students[], int  );
    void display ( int, int, int, double, struct StudentData students[] );
    
    
    int main( void )
    {
    int TotalSemHrCompl = 0;
    int num = 0;
    int i = 0;
    double Sem_gpa = 0.0;
    DATASTU students[1000] = {0}; 
    FILE *incoming;
    
    
    if( (incoming = fopen( "student.dat", "r" )) != NULL ) 
    {
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, incoming) != NULL ) 
    	{
            int res = 0;
            res = sscanf( buff, "%d %s %s %s %d %s",
                &students[num].idnum, students[num].lname, students[num].fname,
                students[num].code, &students[num].credits, students[num].grade );
            if ( res != 6 ) 
    		{
                printf( "Skipping bad record %s", buff );
            } 
    		else 
    		{
                num++;
            }
        }
        fclose( incoming );
    } 
    else 
    {
        printf( "Could not open the file\n" );
        return 1;
    }
    
    
    CalcGradeCredit( students, num );
    
    
    display ( i, num, TotalSemHrCompl, Sem_gpa, students );
    
    
    return 0;
    }
    
    /***************************************** CalcGradeCredit() **************************/
    void CalcGradeCredit ( struct StudentData students[], int num ) 
    {
        int i;
        for ( i = 0 ; i < num; i++ ) 
    	{
            switch ( students[i].grade[0] ) 
    		{
                case 'A':
                case 'a': students[i].gradenum = 4; break;
                case 'B':
                case 'b': students[i].gradenum = 3; break;
                case 'C':
                case 'c': students[i].gradenum = 2; break;
                case 'D':
                case 'd': students[i].gradenum = 1; break;
            }
            students[i].gradecredit = students[i].gradenum * students[i].credits;
        }
    }
    
    
    
    
    /*******************************8 display() ********************************************/
    void display ( int i, int num, int TotalSemHrCompl, double Sem_gpa, struct StudentData students[] )
    {
    for (i = 0; i < num; i++)
    {
    	int course = 0;
    	if (students[num].idnum == students[i+1].idnum)
    	{
    		course = num;
    		num = num+1;
    		printf( "Student Name: %s %s\n", students[course].lname, students[course].fname); 
    		printf( "Student ID number: %d\n\n", students[course].idnum); 
    		printf( "Course\t\tCourse\t\tCourse\n"); 
    		printf( " Code\t\tCredits\t\tGrade\n");
    		printf( "%s\t\t%d\t\t%s\n", students[course].code, students[course].credits, students[course].grade );
    
    			TotalSemHrCompl = students[num - 1].credits + students[num].credits;
    
    		printf( "Total Semester Course Credits Completed: %d\n", TotalSemHrCompl );
    
    			Sem_gpa = (students[num - 1].gradecredit + students[num].gradecredit) / TotalSemHrCompl;
    
    		printf( "Semester Grade Point Average: %.2f\n", Sem_gpa );
    	}
    	else course = 1;
    }
    }
    Thanks...

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    I'm still working on it but at least now it is not in continuous loop! Thanks, Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM