Thread: Error using File Descriptor

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Error using File Descriptor

    HelloI am trying to figure out why I am getting an error (-1) when tryingto write to a file.

    Hereis the function - I did not include the rest of the code - not sureif I am leaving anything else out - I assumed it was a writepermission issue?

    Code:
    #defineDATAFILE "/var/chance.data" //File to store userdata
    
    voidregister_new_player() {  int fd;
    
    printf("-=-={New Player Registration } =-=-\n");
    printf("Enteryour name: ");
    input_name();
    
    player.uid= getuid();
    player.highscore= player.credits = 100;
    
    fd= open(DATAFILE, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
    if(fd= -1)
    fatal("in register_new_player() while opening file");
    write(fd,&player, sizeof(struct user));
    close(fd);
    
    printf("\nWelcometo the Game of Chance %s\n", player.name);
    printf("Youhave been given %u credits.\n", player.credits);
    }
    Hereare my file/folder Permissions

    PHP Code:
    folder:/var:rwxrwxrwt root:root
    file
    :/var/chance.data:-rwxrw---- root:user 


    Imodified the permission for chance.data manually in attempt tofix.

    originalpermissions
    PHP Code:
    -rw-------root:user 


    PHP Code:
    ./game_of_chance:rwsrwxr-x root:root
    game_of_chance
    .c:-rw-rw-r-- user:user 


    Result

    Code:
    user@ubuntu:~/examples$./game_of_chance 
    -=-={New Player Registration } =-=-
    Enteryour name: test
    [!!]Fatal Error in register_new_player() while opening file:Success
    user@ubuntu:~/examples$

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    = is assignment
    == is comparison
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    = is assignment
    == is comparison
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Thank you - I just noticed it while comparing back to the original. I spent a few hours looking at permissions. I hope one day I get a grip on this. Thanks again

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by sjmp View Post
    Thank you - I just noticed it while comparing back to the original. I spent a few hours looking at permissions. I hope one day I get a grip on this. Thanks again
    You should be compiling at the maximum warning level. Any decent compiler nowadays will warn you if you appear to be mixing up = and == if you turn up the warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proxy server using threads - Bad file descriptor error
    By Stiletto in forum C Programming
    Replies: 1
    Last Post: 01-24-2012, 02:29 PM
  2. Replies: 6
    Last Post: 11-30-2011, 12:49 AM
  3. write error with a bad file descriptor
    By techevo in forum C Programming
    Replies: 6
    Last Post: 03-04-2010, 02:58 PM
  4. How to read the file descriptor
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2007, 01:58 AM
  5. Copying Reg Key With Descriptor Error
    By mercury529 in forum Windows Programming
    Replies: 8
    Last Post: 11-12-2005, 06:31 PM

Tags for this Thread