Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!lll-crg!dual!qantel!ihnp4!mhuxn!mhuxm!sftig!sfmag!eagle!ulysses!allegra!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: net.lang.c Subject: Re: What is wrong with this program? Message-ID: <4123@alice.UUCP> Date: Sat, 10-Aug-85 00:40:55 EDT Article-I.D.: alice.4123 Posted: Sat Aug 10 00:40:55 1985 Date-Received: Tue, 13-Aug-85 01:10:59 EDT References: <117@graffiti.UUCP> Organization: Bell Labs, Murray Hill Lines: 47 > #include> main() > { > struct utmp { /* This structure is straight out of utmp.h */ > char ut_line[8]; /* tty name */ > char ut_name[8]; /* user id */ > long ut_time; /* time on */ > }; > FILE *f; > int i; > struct utmp *data; > if (f = fopen ("/usr/adm/wtmp", "r")) { > while ((fread (data, sizeof(struct utmp), 1, f)) > 0) { > putchar('\t'); > for (i = 0; i <= 7; i++) { > putchar(data->ut_line[i]); > } > printf("\n"); > printf("%s %s %ld\n", data->ut_name, > data->ut_line, data->ut_time); > } > fclose(f); > } > } When you are reading into the thing pointed to by data, data doesn't point anywhere because you've never initialized it! Change the declaration of data: struct utmp data; and every place you now use data, use its address instead: while ((fread (&data, sizeof(struct utmp), 1, f)) > 0) { ... putchar (data.ut_line[i]); ... printf ("%.8s %.8s %ld\n", data.ut_name, data.ut_line, data.ut_time); ... Also notice the %.8s -- this defends against the field being completely full. In that case it would not have a null terminator.