我正在编写一个程序,它接受一个包含数据库条目的文件。这些条目都采用相同的格式,数据的顺序相同。文件中的第一个数字是条目数。然后数据如下所示: LastName FirstName StudentID age year GPA expectedGraduationDate
例如:Doe John 12345678 23 大一 4.0 2013
我的问题是年份值。我们应该将它声明为“类”类型,它应该是enum class{freshman, sophomore, junior, senior, grad};
我有一个带有以下声明的头文件:
typedef enum {firstYear, sophomore, junior, senior, grad} class;
然后我的主文件:
#include <stdio.h>
#include <stdlib.h>
#include "class.h"
int main(int argc, char *argv[]){
typedef struct{
int DBrecordID; //ID for each entry, range 0-319
char *last; //student last name
char *first; //student first name
char studentID[8]; //student ID
int age; //student age
class year; //year in school
float gpa; //GPA
int expGradYear; //expected graduation year
}DBrecord;
int numEntries;
DBrecord **record;
char buffer[80];
FILE *fpt;
int c, i;
int j = 0;
//check for invalid file arguments
if(argc != 2){
printf("Number of arguments is invalid\n");
exit(1);
}
//error if unable to open file
if((fpt = fopen(argv[1], "r")) == NULL){
printf("Error: Couldn't open file.\n");
exit(1);
}
//set file pointer to read passed file
fpt = fopen(argv[1], "r");
//scan first int in file and assign to numEntries
//fscanf(fpt, "%d", &numEntries);
//allocate memory for structures, each is 36 bytes
*record = malloc (sizeof (DBrecord) * numEntries);
//loop through each DB entry
do{
for(i=0; i<numEntries; i++){
numEntries = record[i]->DBrecordID;
do{
c=fgetc(fpt);
buffer[j++] = c;
}while(c != ' ');
strcpy(record[i]->last, buffer);
j=0;
do{
c=fgetc(fpt);
buffer[j++] = c;
}while(c != ' ');
strcpy(record[i]->first, buffer);
j=0;
do{
c=fgetc(fpt);
buffer[j++] = c;
}while(c != ' ');
strcpy(record[i]->studentID, buffer);
j=0;
do{
c=fgetc(fpt);
memcpy(c, buffer[j++], 1);
}while(c != ' ');
memcpy(buffer, record[i]->year, 4);
j=0;
do{
c=fgetc(fpt);
buffer[j++] = c;
}while(c != ' ');
memcpy(buffer, record[i]->gpa, 4);
j=0;
do{
c=fgetc(fpt);
buffer[j++] = c;
}while(c != ' ' || c != '\n');
memcpy(buffer, record[i]->expGradYear, 4);
j=0;
}
}while(c != EOF);
return 0;
}
*更新错误
main.c:75:警告:传递 `memcpy' 的 arg 1 使指针从整数而不进行强制转换
main.c:75:警告:传递 `memcpy' 的 arg 2 使指针从整数而不进行强制转换
main.c:77:“memcpy”的参数 2 的类型不兼容
main.c:83:“memcpy”的参数 2 的类型不兼容
main.c:89:警告:传递 `memcpy' 的 arg 2 使指针从整数而不进行强制转换
main.c:94:在“DBrecord”之前解析错误
所以我假设我不能用 memcpy 做我想做的事情,或者我做错了。建议?