I am writing a program that accepts a file with database entries in it. The entries are all in the same format, with the data in the same order. The first number in the file is the number of entries. Then the data looks like this:
LastName FirstName StudentID age year GPA expectedGraduationDate
Ex:
Doe John 12345678 23 freshman 4.0 2013
My issue is with the year value. We are supposed to declare it as type ‘class’, which is supposed to be enum class{freshman, sophomore, junior, senior, grad};
I have a header file with the following declaration:
typedef enum {firstYear, sophomore, junior, senior, grad} class;
Then my main file:
#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;
}
*Updated errors
main.c:75: warning: passing arg 1 of `memcpy’ makes pointer from integer without a cast
main.c:75: warning: passing arg 2 of `memcpy’ makes pointer from integer without a cast
main.c:77: incompatible type for argument 2 of `memcpy’
main.c:83: incompatible type for argument 2 of `memcpy’
main.c:89: warning: passing arg 2 of `memcpy’ makes pointer from integer without a cast
main.c:94: parse error before “DBrecord”
So I’m assuming I can’t do what I’m trying to do with memcpy, or I’m just doing it wrong. Suggestions?
There quite a few errors in the program but for a start
1)
recordshould be of typeDBrecord*notDBrecord**2)
strcpytakes destination as the first argument so this wouldn’t workstrcpy(buffer, record[i]->last);3) you also need to allocate memory for
record[i]->last4)
strcpyis used to copy strings so if you wan’t to store to float or int i.e. gpa etc you need to usememcpyalso the value from buffer should be converted usingstrolstrodalso would recommend to get hold of this book K&R it would be really helpful overtime