fread error (reading a random access file sequentially)

Please assist guys i've been killing myself over it for days trying to figure it out on my own and now the final due date is later on today

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	/*
   Peter Moodie - Parking Space Allocation Software
*/




/*
	Preproccessor Directives( Header Files )
*/
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib>
#include <time.h>
#include <ctype.h>
#include <direct.h>

      
      FILE *cfPtr;
      FILE *file;
      


typedef struct robot ROBOT;

struct robot {
  char *rname;
  char *pass;
};
    ROBOT robots[3];



struct database{
   	char Fname;
      	char lname;
      	int work_hours;
        int acctNum;
   						};

struct database employees;






void create(void)//CODE TO WRITE TO RANDOM ACCESS FILE
{

file = fopen("C:/NixGen PSAS/credit.dat", "r+"); /* create a file for reading and writing */

if(file==NULL) {
printf("An error has occurred.\n");
}
else{
printf ("Enter account Number (1 to 100, 0 to end input)\n ");
scanf("%d", &employees.acctNum);
	while (employees.acctNum != 0) {
printf("Enter Last Name, First Name, Expected Work Hours\n ");
scanf ( "%s%s%f", &employees.lname, &employees.Fname, &employees.work_hours);
fseek(file, (employees.acctNum - 1) * sizeof(struct database), SEEK_SET);
fwrite(&employees, sizeof(struct database), 1, file);
printf("Enter account number\n ");
scanf("%d", &employees.acctNum);
}
}
fclose(file);
getch();
}



//THE ERROR LIES HERE
void viewemp(void)
{

file = fopen("C:/NixGen PSAS/credit.dat", "r");

if(file==NULL) {
printf("Error: can't open file.\n");
}
else {
printf("%6s%16s%11s%15s\n", "Acct", "Last Name", "First Name", "Balance");
while (!feof(file)) {
fread(&employees, sizeof(struct database), 1, file);
if (employees.acctNum != 0)
printf("%d", employees.acctNum);//IT PRINTS THE INTEGER
printf("%s", employees.lname); //THREAD ERROR WHEN IT COMES TO THE STRING
   }
    }
fclose(file);
getch();


            clrscr();
	 login();
            }



Last edited on
lname is a cingle character, not an array of characters

you print lname with %s as a NULL-terminated string, which it is not.
If you want just one character, use %c instead, but you probably
want a string. The current %s print statement will simply dump
memory until it finds a '\0' character, but it will probably hit a
segmentation fault before that

try defining the database structure with proper arrays,
such as

char lname[MAX_LNAME_LENGTH];

where you define the size for instance as

const int MAX_LNAME_LENGTH = 200;

(this comes before the struct database definition)

Thank you so much, i figured it out at school today! xD
But i didnt know why it worked though, thanks a lot for the explanation!
Topic archived. No new replies allowed.