Thread: Files in Unix environment.

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Files in Unix environment.

    Hi, i'm messing around with files in C and i've got a little question. I've done a little bit of searching but I can't seem to come up with anything pertaining to my question in a Unix/Linux environment.

    I want to create a file in a subdirectory, but I'm not exactly sure how to.

    I know to create a file in the current directory, I have to do the following:
    Code:
    FILE *ofp = fopen("Blah","w");
    But how would I do a subdirectory. Suppose i'm at r00t and I want to write to a file in a subdirectory affectionately named, "a".

    I've tried:
    Code:
    FILE *ofp = fopen("//a//Blah","w");
    which just yields a segmentation fault error.

    This is something I should have been able to find w/ google, but I've been unsuccessful. Thanks for any help.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First off, the double slashes aren't necessary--I'm not sure whether POSIX mandates that multiple slashes are compressed into one as happens on systems I'm familiar with, but there's no point to it in any case.

    Next, does the fopen() itself segfault? I would doubt it, but if so, you've done something very fishy beforehand. If trying to write to ofp segfaults, I'd imagine that's because ofp is NULL. While the C standard doesn't mandate that errno be set on fopen() failure, it will be on your unix system. So check it!

    I'd guess you're getting ENOENT. Is the directory "a" a subdirectory of your current working directory? If so, you'd want "a/Blah", not "/a/blah". The latter implies that "a" is in the root directory.

    The point here is, check your return values! Functions tend to let you know when an error occurred, and it's generally a good idea to check that.

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    I forgot to mention something important. The directory "a" doesn't exist yet. I want to create it. I'd imagine that's why my code doesn't work. Dunno why I didn't think of that...

    *Brain fart*

    Is there a way to do this from C?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit]
    I forgot to mention something important. The directory "a" doesn't exist yet. I want to create it. I'd imagine that's why my code doesn't work. Dunno why I didn't think of that...
    That's okay; that's what all of the posters in this thread had assumed. Use the function mkdir() as suggested, but read my note about its portability below. [/edit]

    What cas means is that fopen() returns a NULL pointer on error, and you should check for this.

    I'd guess you're getting ENOENT. Is the directory "a" a subdirectory of your current working directory? If so, you'd want "a/Blah", not "/a/blah". The latter implies that "a" is in the root directory.
    > Suppose i'm at r00t and I want to write to a file in a subdirectory affectionately named, "a".

    man 2 mkdir
    It should also be mentioned that mkdir() is a non-standard function, and so if your code uses it, it won't be ANSI-compatible. I believe mkdir() is a POSIX function, however, so you'll have that measure of portability at least.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Makefile producing a list of files.
    By leonm54 in forum Tech Board
    Replies: 1
    Last Post: 07-23-2007, 09:54 AM
  3. Open files inside directory (UNIX platform)
    By swagatob in forum C Programming
    Replies: 15
    Last Post: 10-25-2004, 12:58 AM
  4. zipping files in UNIX
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2002, 08:46 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM