Loop termination question. I hope this is the right forum

Discussion in 'C Programming' started by grocery_stocker, Jul 19, 2005.

  1. #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?
     
    grocery_stocker, Jul 19, 2005
    #1
    1. Advertising

  2. grocery_stocker

    Chris Dollin Guest

    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.
     
    Chris Dollin, Jul 19, 2005
    #2
    1. Advertising

  3. 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
     
    grocery_stocker, Jul 19, 2005
    #3
  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'.


    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[ ====_____ | |
    >(________|__|_[_________]_|________________________________|

    _/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
    == VxWorks-page: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
     
    Johan Borkhuis, Jul 19, 2005
    #4
  5. grocery_stocker

    Chris Dollin Guest

    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.
     
    Chris Dollin, Jul 19, 2005
    #5
  6. 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
     
    Lawrence Kirby, Jul 20, 2005
    #6
  7. 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
     
    Tommi Johnsson, Jul 23, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. me2u
    Replies:
    2
    Views:
    294
    Saravana [MVP]
    Oct 31, 2003
  2. Chele Sedai

    Very basic beginner question (I hope)

    Chele Sedai, Mar 14, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    380
    Chele Sedai
    Mar 14, 2005
  3. werasm

    for loop termination

    werasm, Nov 22, 2007, in forum: C++
    Replies:
    5
    Views:
    347
    David Harmon
    Nov 22, 2007
  4. Alexey Verkhovsky

    [Ruby Forum] Forum digest by email

    Alexey Verkhovsky, Sep 22, 2004, in forum: Ruby
    Replies:
    0
    Views:
    428
    Alexey Verkhovsky
    Sep 22, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    407
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page