Can anybody help me understand why this isn't working correctly, I'm attempting to scan in the data from a command line file. I am storing the data in structures but every time I compile and run, the program never ends, it gets caught in a never ending loop, but I don't know why.
Code:
#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
#define ARR_SIZE 10
typedef struct
{
char fName[STR_SIZE];
char lName[STR_SIZE];
}NAME;
typedef struct
{
char month[STR_SIZE];
int date;
int year;
}DATE;
typedef struct
{
NAME name;
int level;
char ssn;
DATE birthdate;
}STUREC;
int fillArray(FILE *inf, STUREC arr[]);
void sortArray(STUREC arr[], int numEls);
void swap(STUREC *stu1, STUREC *stu2);
void printArray(STUREC arr[], int numEls);
int main(int argc, char *argv[])
{
int numEls;
STUREC arr[ARR_SIZE];
FILE *inf;
inf = fopen(argv[1], "r");
numEls = fillArray(inf, arr);
printArray(arr, numEls);
//sortArray(arr, numEls);
printArray(arr, numEls);
fclose(inf);
return 0;
}
int fillArray(FILE *inf, STUREC arr[])
{
int ElsCnt = 0;
int ef;
printf("\nProgramers Name: \n\n");
//while (inf != NULL)
while ((ef= fgetc(inf)) !=EOF)
{
fscanf(inf, "%s %s %d %s %s %d, %d", arr->name.fName,
arr->name.lName,
&arr->level, &arr->ssn,
arr->birthdate.month,
&arr->birthdate.date,
&arr->birthdate.year);
ElsCnt ++;
}
return ElsCnt;
}
void printArray(STUREC arr[], int numEls)
{
printf("%20s %10s %15s %15s", "Name", "Level", "SSN", "Birthdate\n");
printf("%20s %10s %15s %15s", "----", "-----", "---", "---------\n");
/*printf("%10s%10s%10d%15s%10s/%2d/%2d", arr->name.fName,
arr->name.lName,
arr->level, arr->ssn,
arr->birthdate.month,
arr->birthdate.date,
arr->birthdate.year);
*/
}
I'm guessing it has to do with the "while (inf !=NULL)" line, But I don't know how to implement eof into c? ...or can you?
EDIT: Ok, fixed that (I think) (check code)
but now I'm having trouble storing the elements in an array. I'd assume you just use a for loop?