fopen

This is a discussion on fopen within the C Programming forums, part of the General Programming Boards category; Hello, I'm using Linux. My .c is in ~/src and the file I want to use is in /$home. But ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    fopen

    Hello,

    I'm using Linux. My .c is in ~/src and the file I want to use is in /$home.
    But when I write fopen("$HOME/file","rb"), the result is 0.
    Hence my question : how to use fopen to open a file that is in the home directory ?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    You have to do getenv() to read what $HOME expands to, then create a pathname of your own (strcpy, strcat, sprintf).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    Thanks for the answer.
    That seems quite difficult. Isn't there an easier way to write the path correctly once and for all ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Use a different language which automatically expands environment variables for you perhaps?

    C is low level, you don't have a lot of creature comforts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple fopen not working...
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 10-12-2009, 01:48 PM
  2. error fopen with existing file
    By cfdprogrammer in forum C Programming
    Replies: 8
    Last Post: 10-02-2009, 01:43 PM
  3. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  4. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  5. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM

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