-2

I have question about reading data from file to structures when I tried to run this code i get unhandled exception Access violation reading location 0xcccccce0, The error occur inside the getData function, Why am I getting this error, how Should I fix the code ?

this is my input file

4
A,10
B,12
C,60
D,120
tutorY

my intention in the getData function was to first read the first line to get then number 4, then use that number to allocate for student structure and then read the next four lines of the file in to student structure fields and then read the last line into tutorname feild in the TUTOR structure.

thank in advance

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "queue.h"
    #include "stack.h"

    #define RECORDS_SIZE 100
    #define NAME_SIZE    20

    typedef struct Student
    {
        char nameStudent[NAME_SIZE];
        int  TimeIn;
        int  TimeUpdate;
    }STUDENT;

    typedef struct TUTOR
    {
        char nameTutor[NAME_SIZE];
        int TutorTIme;
        STUDENT *ptr;
    }TUTOR;


    QUEUE *queue1;
    STACK *stack1;

    void getData(STUDENT *studentArr[RECORDS_SIZE], TUTOR tutorArr[1]);

    int main (void)
    {

        STUDENT *studentArr[RECORDS_SIZE];
        TUTOR tutorArr[1];
        FILE *fp = NULL;

        getData(studentArr, tutorArr);

        return 0;
    }


    void getData(STUDENT *studentArr[RECORDS_SIZE], TUTOR tutorArr[1])
    {
        FILE *fp;
        char fileName[NAME_SIZE];
        char buffer[RECORDS_SIZE];
        int first = 0;
        int count = 1;

        printf("Enter file name: ");
        gets(fileName);
        fp = fopen(fileName, "r");
        if (fp == NULL)
        {
           printf("Error! The file does not exist!\n");
        }

        fscanf(fp,"%d",&first);
        *studentArr = (STUDENT*) malloc(first*sizeof(STUDENT));
        while( fgets(buffer, first +1, fp) != NULL)
        {          
           if (count <= first)
           {
              sscanf(buffer, "%[,]%d", studentArr[count]->nameStudent, studentArr[count]->TimeIn);
              printf("%s,%d", studentArr[count]->nameStudent, studentArr[count]->TimeIn);  
           }
           else
              sscanf(buffer, "%s", tutorArr[count].nameTutor);
               count++;
        }

        return;
    }
4

3 回答 3

2

我发现了几个问题。

  1. 第一次读取 fscanf 将读取第一个数字,然后将剩余的行 ("\n") 留给第一次调用 fgets 来获取

  2. 更重要的是,studentArr 是一个指针数组,大概每个学生都有一个元素,但是 malloc 只分配了 sudentArr 中的第一个指针,其他的都包含垃圾,这导致了访问冲突。

于 2013-04-24T06:45:38.960 回答
0

还有一些问题:

  1. count应该初始化为 0 而不是 1 因为你的数组是零索引的。
  2. 你没有count在你的循环中增加
  3. 为循环内的每个学生分配内存studentArr[count] = new Student();
  4. 更喜欢strtoksscanf` 将缓冲区拆分为字段。处理字符串后面的逗号要容易得多。
于 2013-04-24T07:13:38.260 回答
0

像这样修复

void getData(STUDENT **studentArr, TUTOR tutorArr[1]);//change

int main (void)
{

    STUDENT *studentArr;//change
    TUTOR tutorArr[1];
    FILE *fp = NULL;//not use

    getData(&studentArr, tutorArr);//change

    return 0;
}


void getData(STUDENT **studentArr, TUTOR tutorArr[1])
{
    FILE *fp;
    char fileName[NAME_SIZE];
    char buffer[RECORDS_SIZE];
    int first = 0;
    int count = 1;

    printf("Enter file name: ");
    gets(fileName);//has risk
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
       printf("Error! The file does not exist!\n");
       return;//add, can't continue
    }

    fscanf(fp,"%d\n",&first);//add, consumption '\n'
    *studentArr = (STUDENT*) malloc(first*sizeof(STUDENT));
    while( fgets(buffer, RECORDS_SIZE, fp) != NULL)//change buffer size
    {          
       if (count <=first)//
       {
          sscanf(buffer, "%[^,],%d", (*studentArr)[count-1].nameStudent, &(*studentArr)[count-1].TimeIn);//add, `,` and -1 to count is 1 origin, `&` need for "%d" 
          printf("%s,%d\n", (*studentArr)[count-1].nameStudent, (*studentArr)[count-1].TimeIn);  
          ++count;//need count up
       }
       else
          sscanf(buffer, "%s", tutorArr[0].nameTutor);
    }

    return;//need allocate record size return to main
}
于 2013-04-24T07:13:49.637 回答