不在结构

时间:2015-09-14 18:27:13

标签: c struct fread

我一直在尝试打开程序添加一些值来构造将它们写入文件,然后在重新打开文件时再读取它们。虽然仍然在程序的相同运行中,fread工作(或者可能不会编辑成员),但是当我尝试重新打开时,打印的值是垃圾。我做错了什么,或者做不到那种方式做事。在fwrite之后的文件中它包含“SOH NUL NUL NUL ABCD NUL 1234.000 NUL(x11)”

在prototype.c中有一个加载的函数原型,但我还在main中编写了一个版本(注释掉了同样的函数调用),认为这个问题可能是指针问题但仍然没有运气

// main.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS        100
#define MAX_COURSES         25
#define COURSES_PER_STUDENT 4
#define MAX_GRADES          10
#define STUDENTS_PER_COURSE 20

struct course_record
{
    long course_id;
    char course_name[5];
    char course_number[9];
    int number_of_students;
    int course_average;
};

struct student_record
{
    int student_id;
    char first_name[15];
    char last_name[15];
    int number_of_courses;
    int averages[COURSES_PER_STUDENT];
};

struct enrollment_record
{
    int id;
    struct student_record *student;
    struct course_record *course;
    int grades[MAX_GRADES];
    int average;
    struct enrollment_record *next;
};

/****************************************************************************************************************
*                                                       *
            COURSE STRUCTURE FUNCTION PROTOTYPES
*                                                       *
*****************************************************************************************************************/

int course_exist(struct course_record *a, char *s, char *t, long n);

int check_number(char *s);

void add_course(struct course_record *a, long *n);

void course_save(struct course_record *a, long *n);

void course_load(struct course_record a, long *n);

/****************************************************************************************************************
*                                                       *
            STUDENT STRUCTURE FUNCTION PROTOTYPES
*                                                       *
*****************************************************************************************************************/



/****************************************************************************************************************
*                                                       *
            ENROLLMENT STRUCTURE FUNCTION PROTOTYPES
*                                                       *
*****************************************************************************************************************/



/****************************************************************************************************************
*                                                       *
            MAIN FUNCTION PROTOTYPES
*                                                       *
 *****************************************************************************************************************/

void convert(char *s);`

// prototype.c

#include "main.h"

/****************************************************************************************************************
*                                                       *
            COURSE STRUCTURE FUNCTION PROTOTYPES
*                                                       *
*****************************************************************************************************************/

//search course function

//check for existing courses
int course_exist(struct course_record *a, char *s, char *t, long n)
{
    int i;

    for(i = 0; i < n; i++)
    {
        if(strcmp(a[i].course_name, s) == 0)
        {
            if(strcmp(a[i].course_number, t) == 0)
                return 0;
            else
                return 1;
        }
    }
}

//check input format of course number_of_students
int check_number(char *s)
{
    int i;

    for(i = 0; i < 9; i++)
    {
        if((isdigit(s[i])) != 0)
            return 1;
        else if(s[4] != '.')
            return 1;
        else
            return 0;
    }       
}

//add course function
void add_course(struct course_record *a, long *n)
{
    char course_num_temp[9];
    char course_name_temp[5];

    printf("\nEnter Course Name [ABCD]: ");
    scanf("%s", course_name_temp);
    printf("\nEnter Course Number [1234.000]:");
    scanf("%s", course_num_temp);

    a[*n].course_id = *n +1;
    strcpy(a[*n].course_name, course_name_temp);
    strcpy(a[*n].course_number, course_num_temp);
    a[*n].number_of_students = 0;
    a[*n].course_average = 0;
}   

//save function for courses
void course_save(struct course_record *a, long *n)
{
    int i;
    FILE *course_file;

    course_file = fopen("course.dat", "wb");
    for(i = 0; i < *n; i++)
    {
        fwrite(&(a[i]), sizeof(struct course_record), 1, course_file);
    }
    fclose(course_file);
}

//load function for courses
void course_load(struct course_record a, long *n)
{
    int i;
    char temp[40];
    FILE *course_file;

    course_file = fopen("course.dat", "rb");
    if((course_file = fopen("course.dat", "rb")) == '\0')
        course_file  = fopen("course.dat", "w+b");
    fseek(course_file, 0L, SEEK_END);
    *n = (ftell(course_file))/((long)sizeof(struct course_record));
    printf("\nftell:%ld sizeof:%ld\n", ftell(course_file),     (long)sizeof(struct course_record));
    printf("\nn:%ld\n", *n);
    for(i = 0; i < *n; i++)
    {
        fread(&temp, sizeof(struct course_record), 1, course_file);
        /*printf("%ld %s %s %d %d", a[i].course_id,
                                 a[i].course_name,
                                 a[i].course_number,
                                 a[i].number_of_students,
                                 a[i].course_average);*/
    }
    fclose(course_file);
}


        /******************************************************************************* *********************************
*                                                       *
            STUDENT STRUCTURE FUNCTION PROTOTYPES
*                                                       *
   *****************************************************************************************************************/

//search student function

//add student function

           /****************************************************************************************************************
*                                                       *
            ENROLLMENT STRUCTURE FUNCTION PROTOTYPES
*                                                       *
        *****************************************************************************************************************/

//search enrollment function

//create enrollment function

   /****************************************************************************************************************
*                                                       *
        MAIN FUNCTION PROTOTYPES
*                                                       *
    *****************************************************************************************************************/

//check input function for characters

//check input function for integers

//convert characters to uppercase
void convert(char *s)
{
    int i, n;

    n = strlen(s);

    for(i = 0; i < n; i++)
        toupper(s[i]);
}

的main.c

#include "main.h"

int main()
{
    //global variable declarations
    int i, main_menu;

    //variable declarations for course
    long course_pos;
    struct course_record courses[MAX_COURSES];
    FILE *course_file;

    //variable declarations for student
    int student_pos;

    //variable declarations for enrollment
    int enroll_pos;

    do
    {
        printf("\nMAIN MAIN\n\n");
        printf("1)  Add Course\n");
        printf("8)  Print Courses\n");
        printf("11) Save\n");
        printf("12) Load\n");
        printf("20) Exit\n");
        printf("Please make a selection: ");
        scanf("%d", &main_menu);

        switch(main_menu)
        {

            case 1:

                add_course(courses, &course_pos);
                course_pos += 1;
                printf("\n%ld\n", course_pos);
                break;

            case 8:

                printf("\nTest\n");

                if(course_pos == 0)
                    printf("No Course Records Exist");
                else
                {
                    for(i =0; i < course_pos; i++)
                    {
                        printf("%ld %s %s %d %d\n", courses[i].course_id, 
                                                   courses[i].course_name,
                                                   courses[i].course_number,
                                              courses[i].number_of_students, 
                                              courses[i].course_average);
                    }
                }
                break;

            case 11:

                course_save(courses, &course_pos);
                printf("\nCourses Saved\n");
                break;

            case 12:

                //course_load(*courses, &course_pos);
                //printf("\nCourses Loaded\n");


                ///*
                course_file = fopen("course.dat", "rb");
                if((course_file = fopen("course.dat", "rb")) == '\0')
                    course_file  = fopen("course.dat", "w+b");
                fseek(course_file, 0L, SEEK_END);
                course_pos = (ftell(course_file))/((long)sizeof(struct     course_record));
                printf("\nftell:%ld sizeof:%ld\n", ftell(course_file),    (long)sizeof(struct course_record));
            printf("\nn:%ld\n", course_pos);
            for(i = 0; i < course_pos; i++)
            {
                fread(&courses[i], sizeof(struct course_record), 1, course_file);
                printf("%ld %s %s %d %d\n", courses[i].course_id,
                                          courses[i].course_name,
                                          courses[i].course_number,
                                          courses[i].number_of_students,
                                          courses[i].course_average);
            }
            fclose(course_file);
            //*/
            break;

        case 20:

            printf("\nClosing Program\n");
            return 0;

        default:

            printf("Invalid Input");
            break;

    }
}while(main_menu !=20);

return 0;
}

0 个答案:

没有答案