problem with gets and EOF

Hi guys,
i'm trying to make a simple C program, but i have problems with EOF!
i need to get at most 200 information of students including firstName, lastName and studentCode.
firstName length is 20 at most
lastName maximum length is 50 and maximum size of studentCode is 7 chars.
i should read inputs until EOF entered or reached at 200.
*Note: names may contain spaces between them.
**Note: i need to do it in C language and i can not use any c++ futures.
sorry for bad English,
and thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdio.h>

struct Student
{
    char firstName[20];
    char lastName[50];
    char studentCode[7];
    int flag;
};

void studentInitializeser(struct Student* student);

int main(void)
{
    struct Student students[200];
    int i = 0;
    for(i = 0 ; i < 200 ; i ++)
        studentInitializeser(&students[i]);
    i = 0;
    gets(students[0].firstName);
    while(students[i].firstName != EOF)
    {
        gets(students[i].lastName);
        gets(students[i].studentCode);
        students[i].flag = TRUE;
        i++;
        gets(students[i].firstName);
    }

    return 0;
}


void studentInitializeser(struct Student *student)
{
    student -> flag = FALSE;
    int i = 0;
    for(i = 0 ; i < 20 ; i ++)
        student->firstName[i] = " ";
    i = 0;
    for(i = 0 ; i < 50 ; i ++)
        student->lastName[i] =" ";
    i = 0;
    for(i = 0 ; i < 7 ; i ++)
        student->studentCode[i] = -1;
}
while(students[i].firstName != EOF)

You can't compare a char array with an integer.

student->firstName[i] = " ";

To initialize a char you need single quotes.
I don't usually use C, so I may be a bit rusty or mistaken here.

First, the function gets() is deprecated, that is it is well on the way to becoming obsolete, and should not be used. The issue here is that there is no check on the numbers of characters read, so buffer overflow may occur.
http://www.cplusplus.com/reference/cstdio/gets/

The suggested alternative is fgets(stdin). This allows the maximum length of the input buffer to be specified. However, there are differences in behaviour which must be handled too.
http://www.cplusplus.com/reference/cstdio/fgets/
thanks for your answer @Thomas1965 and @Chervil,
according to your comments, i changed the code to this version and commented exactly where i have problem with getting input until EOF entered.
when i enter "^Z" as the EOF, console is still waiting for input.(my system is Ubuntu).
here is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdio.h>

struct Student
{
    char firstName[20];
    char lastName[50];
    char studentCode[7];
    int flag;
};

void studentInitializeser(struct Student* student);

int main(void)
{
    struct Student students[200];
    int i = 0;
    for(i = 0 ; i < 200 ; i ++)
        studentInitializeser(&students[i]);
    i = 0;
    while(fgets(students[i].firstName,20,stdin) != NULL && i < 200) /* here i have problem, how can i check if EOF entered or not? */
    {
        fgets(students[i].lastName,50,stdin);
        fgets(students[i].studentCode,7,stdin);
        students[i].flag = TRUE;
        i++;
    }

    return 0;
}


void studentInitializeser(struct Student *student)
{
    student -> flag = FALSE;
    int i = 0;
    for(i = 0 ; i < 20 ; i ++)
        student->firstName[i] = ' ';
    i = 0;
    for(i = 0 ; i < 50 ; i ++)
        student->lastName[i] =' ';
    i = 0;
    for(i = 0 ; i < 7 ; i ++)
        student->studentCode[i] = -1;
}
Last edited on
I'm kind of embarrassed to post this as I don't normally write in C, and I think my code is a bit of a mess. I tested it in Windows. I ran the program from the command line and passed it the input from a text file, but it should also work if the user enters the data at the keyboard.

Perhaps it will at least give you some ideas.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student
{
    char firstName[21];
    char lastName[51];
    char studentCode[8];
    int  flag;
};

int readInput(char *, int len);

#define  MAXNUM 200
int main()
{
    
    struct Student students[MAXNUM];  
    
    int count = 0;
    
    while (count < MAXNUM && readInput(students[count].firstName,  21))  
    {
        readInput(students[count].lastName,   51);
        int ok = readInput(students[count].studentCode, 8);
        
        if (!ok)
        {
            printf ("Error reading student input\n");
            printf("%20s, %50s %7s\n    ------ error ---- \n", students[count].firstName, 
                students[count].lastName, students[count].studentCode  );

            break;
        }
        
        count++; // ------------ important --------------------
        
//        if (feof(stdin))
//        {
//            printf ("End of file reading student input\n");
//            break;
//        }
    }
        

    
    for (int i=0; i<count; i++)
    {
        printf("%20s, %50s %7s\n", students[i].firstName, students[i].lastName, students[i].studentCode  );
    }
    

    puts("\n -- Done --\n");
       
    return 0;
}

int readInput(char * field, int len)
{
    int ch;
    static char buf[1024];
    char * ok = fgets(buf,  len, stdin);       // allow for newline at end
    int n = strlen(buf);
    if (buf[n-1] == '\n')                      // remove newline if present
        buf[n-1] = '\0';
    else
        do {
            ch = fgetc(stdin);
        }   while (ch != '\n' && ch != EOF);   // ignore until newline found   
        
    strncpy(field, buf, len);
//  if (n > len)                               // add missing null terminator if required
//      field[len-1] = 0; 
     
    return (ok != NULL);
    
}

Last edited on
Topic archived. No new replies allowed.