0

When I try to read the files to my structure using case 1 in my menu the programme just exits.

I tried reading in both files into the structures as separate functions but both seem to end my function and when I tried to put them in main the same happened again. At this stage I've pretty much run out of ideas as I'm pretty new to c language and only have a few weeks on it.

FILE* fp;

/* structured type definition */
typedef struct empl
{
    char name[20];
    char surn[20];
    char PPS[20];
    char nat[20];
    int age;
    int mar;
    int cld;

} empl_type;

typedef struct benf
{
    char PPS[20];
    int sal;
    int pen;
    int hlt;
    int all;

} benf_type;


/* 1-dimensional array that stores benefits related data for all registered employees*/
    benf_type benf[5000];
    /* 1-dimensional array that stores employees related data */
    empl_type empl[5000];
    /* no current registered empl (no records) */
    int noempl = 0;
    /* no current benefits (no records) */
    int nobenf = 0;


/* main function */
int main()
{

    int ans = 1;

    /* welcome message */
    printf("Welcome to the program!\n");

    while(ans != 6)
    {
        printf("Menu\n");
        printf("1-Read data from files\n");
        printf("2-Apply allowance\n");
        printf("3-Search\n");
        printf("4-Print to screen\n");
        printf("5-Write data in files\n");
        printf("6-Exit\n");
        scanf("%d", &ans);

        switch(ans)
        {
            case 1: read_files(); break;
            case 2: apply_allow(benf, nobenf); break;
            case 3: search_PPS(); break;
            case 4: print_screen(); break;
            case 5: write_file(); break;
        }
    }
    printf("Goodbye!\n");

    return (EXIT_SUCCESS);
}

void read_files()
{

        noempl = read_empl_file("e.txt", empl);

        nobenf = read_benf_file("b.txt", benf);

}

int read_empl_file(char* file_name, empl_type empl[])
{

    char ch;

    /* open file */
    fp = fopen(file_name, "r");
    while(!feof(fp))
    {
        /* read on record from file */
        fscanf(fp,"%s%c%s%c%s%c%s%c%d%c%d%c%d%c", empl[noempl].name, &ch, empl[noempl].surn, &ch, empl[noempl].PPS, &ch, empl[noempl].nat, &ch, &empl[noempl].age, &ch, &empl[noempl].mar, &ch, &empl[noempl].cld, &ch);

        /* increase the number of records read*/
        noempl++;
    }

    /* close file */
    fclose(fp);
    return noempl;
}

int read_benf_file(char* file_name, benf_type benf[])
{

    char ch;

    /* open file */
    fp = fopen(file_name, "r");
    while(!feof(fp))
    {
        /* read on record from file */
        fscanf(fp,"%s%c%d%c%d%c%d%c%d%c", benf[nobenf].PPS, &ch, &benf[nobenf].sal, &ch, &benf[nobenf].pen, &ch, &benf[nobenf].hlt, &ch, &benf[nobenf].all, &ch);

        /* increase the number of records read*/
        nobenf++;
    }

    /* close file */
    fclose(fp);
    return nobenf;
}
Vesprs
  • 1
  • 1
    Welcome to Stack Overflow! [Why `while (!feof(fd))` is wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Apr 10 '19 at 20:32
  • What does the file content look like? – Barmar Apr 10 '19 at 20:33
  • b.txt is 5 columns of 1234567D 37500 20 10 0 3234567F 40000 20 10 0 5234567G 42000 20 10 0 6234567N 45000 20 10 0 8234567B 47000 20 10 0 4234567F 50000 20 10 0 5234567M 55000 20 10 0 7734567E 60000 20 10 0 2254893R 75000 20 10 0 and e.txt is 7 columns of Jimmy OBrien 1234567D Irish 40 1 2 David Keogh 3234567F Irish 61 0 0 David Hanahoe 5234567G Irish 48 1 2 Tom Byrne 6234567N Irish 62 1 4 Thomas Collins 8234567B Irish 35 0 0 Aoife Murphy 4234567F Irish 63 1 3 Joan OBrien 5234567M Irish 36 1 1 Maria Ionescu 7734567E Romanian 43 1 2 Celine Keogh 2254893R Irish 38 1 0 – Vesprs Apr 10 '19 at 20:41
  • 1
    Don't use `%c` to skip over whitespace between items. Just put a space in the format string. – Barmar Apr 10 '19 at 20:43
  • 1
    Put the space *before* each item in the format string, it will then do the right thing when going past newlines. – Barmar Apr 10 '19 at 20:44
  • One of the first things you need to learn about is debuggers. You can step through the program this way, and see what it's doing when it crashes. – Barmar Apr 10 '19 at 20:46
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Barmar Apr 10 '19 at 20:47
  • It compiles without any errors but the programme still exits when I select read_files. – Vesprs Apr 10 '19 at 20:48
  • 1
    Compilers can't detect problems that only occur during runtime. That's why you need to learn how to debug. – Barmar Apr 10 '19 at 20:49
  • Ok thanks Barmar I'll give that a try. – Vesprs Apr 10 '19 at 20:51

0 Answers0