473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Loop termination question. I hope this is the right forum

#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?

Nov 15 '05 #1
6 1331
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.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #2
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

Nov 15 '05 #3
grocery_stocker wrote:
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[ ====_____ | jo***@borkhuis.com |(________|__|_[_________]_|________________________________|

_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks-page: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
Nov 15 '05 #4
grocery_stocker wrote:
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`?

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #5
On Tue, 19 Jul 2005 05:15:41 -0700, grocery_stocker wrote:
#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

Nov 15 '05 #6
Chris Dollin wrote:
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.


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

- jt
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
23
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x...
6
by: Shill | last post by:
I have several questions. In C, AFAIU, a for loop is just syntactic sugar for a while loop. for (i1; i2; i3) i4; is equivalent to i1 while (i2) {
8
by: Smkmdb1 | last post by:
Hi everyone, I'm a student of ASP.net. Being an ex Perl/CGI programmer who made extra cash making scripts and selling them on the web, I was wondering if one typically uses this format with...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
18
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.