#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]);
int main (void)
{
STUDENT *studentArr[RECORDS_SIZE];
FILE *fp = NULL;
getData(studentArr);
return 0;
}
void getData(STUDENT *studentArr[RECORDS_SIZE])
{
FILE *fp;
char fileName[NAME_SIZE];
char buffer[RECORDS_SIZE];
int count = 0;
printf("Enter file name: ");
gets(fileName);
fp = fopen(fileName, "r");
if (fp == NULL)
{
printf("Error! The file does not exist!\n");
}
fgets(buffer, RECORDS_SIZE, fp);
*studentArr = (STUDENT*) malloc(buffer[0]*sizeof(STUDENT));
while (fgets(buffer, RECORDS_SIZE, fp) != NULL)
{
printf("%s", buffer[count]);
*studentArr[count]->nameStudent = (char*) malloc(strlen(buffer)*sizeof(char));
studentArr[count]->TimeIn = (int) malloc(strlen(buffer)*sizeof(int));
sscanf(buffer,"%[,],%d", *studentArr[count]->nameStudent, &studentArr[count]->TimeIn);
printf("%s%d\n", studentArr[count]->nameStudent, &studentArr[count]->TimeIn);
count++;
}
return;
}
有一个警告说,该赋值从没有强制转换的指针中生成整数 [默认启用]| 在我为 *studentArr[count]->nameStudent 分配内存的行上,为什么会收到此警告?
这就是我的文件的样子
4
A,10
B,12
C,60
D,120
tutorY
我尝试在第一行中读取,然后使用第一行上的数字来分配指向结构数组的指针并继续读取,然后分配结构的其余成员
我认为我的while循环错误,可能是我不应该调用fgets然后在while循环中重用它,因为它似乎在那里有错误。如何修复此功能?
预先感谢