Failing to open() a file

This is a discussion on Failing to open() a file within the C Programming forums, part of the General Programming Boards category; Hi - I'm using the open() function to open an existing file on linux. I keep getting a failure return ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    16

    Failing to open() a file

    Hi - I'm using the open() function to open an existing file on linux. I keep getting a failure return value (-1 in this case) when trying to do so:
    Code:
    int result = open("/tmp/my_file" , O_RDONLY);
    The file exists and I have permissions on it. This is driving me crazy - I'd appreciate any help on this - thanks.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I always thought C uses fopen() to open files.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    open() is used, also, in linux
    Is this the first time it is open it in the program? Can you generally use the file from the OS? Like try editing it and see

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    In that case "man open" yields the following:
    Otherwise, -1 is returned, errno is set to indicate the error
    it then goes on to list the errors that could occur with open(). On Linux, man should always be your first port of call for these things, in other words, RTFM.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    try using perror() to if 'result' is negative. it will give you more information on the error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21