2012-04-18 47 views
0

我在写一个程序,它接受一个带有数据库条目的文件。条目全部采用相同的格式,数据的顺序相同。文件中的第一个数字是条目的数量。然后,该数据是这样的: 姓氏名字StudentID年龄一年GPA expectedGraduationDateC - 使用fgetc在enum'class'中存储值

例: Doe的约翰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:warning:传递`memcpy'的arg 1使得整型指针没有转换

main.c中:75:警告:传递`的memcpy的ARG 2 '时将整数指针,未作铸造

main.c中:77:不兼容的类型为`的memcpy的参数2'

主。 C:83:不兼容的类型为`的memcpy的参数2 '

main.c中:89:警告:传递的`的memcpy ARG 2' 时将整数指针,未作铸造

main.c中:94:解析“DBrecord”前的错误

所以我假设我不能做我想用memcpy做什么,或者我只是做错了。建议?

回答

1

有不少的程序,但一开始

1)record错误应该是DBrecord*类型不DBrecord**

2)strcpy采取目的地作为第一个参数所以这是行不通的 strcpy(buffer, record[i]->last);

3)您还需要为0分配内存

4)strcpy用于复制字符串,所以如果你不想存储为float或int即GPA等你需要使用memcpy也从缓冲区中的值应转换使用strolstrod

也建议得到这本书K&R的保持这将是非常有益的加班

+0

我敢肯定,我宣布'记录'正确。我需要一个双指针指向结构的一部分,'DBrecord'和'DBrecord.last'或'DBrecord.first'等等。感谢strcpy的提示,我发现我得到的是反向参数。至于内存分配,我用'* record = malloc(sizeof(DBrecord)* numEntries);'为什么我需要分配更多的内存?最后,我想我需要帮助理解'memcpy'。为什么缓冲区需要转换,如果'memcpy'不关心什么类型被传递?谢谢您的帮助!我知道我看起来很笨。 – manalishi 2012-04-18 14:41:44

+0

@manalishi让我们先使用DBrecord,当你使用'DBrecord ** record'和'* record = malloc'。记录不是初始化的,所以'* record'会seg故障,我想你打算的是DBrecord的数组,它是'DBrecord *',因为数组衰减到指针。其次,当为last_name等字段分配sizeof(DbRecord)时,您分配的是指针的大小,但现在需要将指针“last_name”初始化为有效地址,因此需要分配。我会建议先尝试让程序编译,然后逐个使用调试器来解决逻辑问题 – keety 2012-04-18 14:52:56

+0

对不起,但我没有关注你。你会看看[我的另一篇文章关于此](http://stackoverflow.com/questions/10076261/c-build-dynamically-allocated-array-of-pointers-to-structures-filled-with-inpu)并让我知道你是否仍然觉得这是错的?评论似乎同意。 – manalishi 2012-04-18 15:14:21

0

为什么你会从int尝试strcpy字符串复制),像expGradYear

strcpy适用于STRINGS。如果你想为int追加到缓冲区,使用memcpy内存复制)