簡體   English   中英

C-使用fgetc將值存儲在枚舉“類”中

[英]C - using fgetc to store value in enum 'class'

我正在編寫一個程序,該程序接受其中包含數據庫條目的文件。 條目均采用相同格式,數據順序相同。 文件中的第一個數字是條目數。 然后,數據將如下所示:LastName FirstName StudentID年齡GPA預期的GraduationDate

例如: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做我想做的事情,或者我做錯了。 有什么建議嗎?

程序中有很多錯誤,但首先

1) record應為DBrecord*類型DBrecord*不是DBrecord**

2) strcpy將destination作為第一個參數,因此這不會起作用strcpy(buffer, record[i]->last);

3)您還需要為record[i]->last分配內存

4) strcpy用於復制的字符串,所以如果你wan't存儲浮動或INT即GPA等需要使用memcpy還從緩存器中的值應使用被轉換strol strod

也建議得到這本書的保持K&R這將是非常有益的加班

您為什么要嘗試從intexpGradYear那樣strcpy字符串副本)?

strcpy適用於STRINGS。 如果要將int附加到緩沖區,請使用memcpyMEMory Copy)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM