Loop termination question. I hope this is the right forum

G

grocery_stocker

#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

static int user_info(struct utmp *user){

setutent();

printf("%12s %8s %22s %14s",
user->ut_user,
user->ut_line,
user->ut_host,
ctime(&user->ut_time)
);
endutent();

return 0;
}

int main(void) {
struct utmp log_file;

FILE *fp;

print_banner();

for((fp =
fopen("/var/log/wtmp","r"))!=0;fread(&log_file,sizeof(struct
utmp),1,fp) !=0;) {

user_info(&log_file);


/*if(!feof(fp)){
perror( "End of File \n" );
exit(EXIT_SUCCESS);
}

if(fp == NULL) {
fprintf(stderr,"Can't open file\n");
exit(EXIT_SUCCESS);
}*/
fclose(fp);
}

}


Why does this only output one line from the following:
$./emulate
Name Terminal Hostname Time
reboot ~ 2.6.4-52-default Tue Jun 21 07:38:30 2005
End of File
: Permission denied

Why doesn't the loop go through the entire file?
 
C

Chris Dollin

grocery_stocker wrote:

for((fp =
fopen("/var/log/wtmp","r"))!=0;fread(&log_file,sizeof(struct
utmp),1,fp) !=0;) {
(snip)

fclose(fp);

You close `fp`, then you try and `fread` from it.

Oops. All bets are off.
 
G

grocery_stocker

Okay, then one last question.

How come when I change the path from

"/var/log/wtmp" to something like "/home/cdalten/non-existant-file.txt"
I get Segmentation fault? I thought the error would be stored in 'fp'.

Chad
 
J

Johan Borkhuis

grocery_stocker said:
Okay, then one last question.

How come when I change the path from

"/var/log/wtmp" to something like "/home/cdalten/non-existant-file.txt"
I get Segmentation fault? I thought the error would be stored in 'fp'.

Because you always do a read, even if the open fails. Just take a good
look at the first part of your for statement.

(SPOILER: your compare doesn't do anything......)

Kind regards,
Johan

--
o o o o o o o . . . _____J_o_h_a_n___B_o_r_k_h_u_i_s___
o _____ || http://www.borkhuis.com |
.][__n_n_|DD[ ====_____ | (e-mail address removed) |
>(________|__|_[_________]_|________________________________|
_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks-page: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
 
C

Chris Dollin

grocery_stocker said:
Okay, then one last question.

How come when I change the path from

"/var/log/wtmp" to something like "/home/cdalten/non-existant-file.txt"
I get Segmentation fault? I thought the error would be stored in 'fp'.

Did you read the spec for `fopen`?
 
L

Lawrence Kirby

#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

static int user_info(struct utmp *user){

setutent();

This is very Unix specific, a good place to ask would be
comp.unix.programmer.

Lawrence
 
T

Tommi Johnsson

Chris said:
grocery_stocker wrote:





You close `fp`, then you try and `fread` from it.

Oops. All bets are off.

.... you should also open binary file as binary file.

- jt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top