Thread: File/Directory manipulation

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    File/Directory manipulation

    I would like to be able to do file/directory manipulation in C.

    Simple and cross-platform would be great, otherwise it should work in Linux.

    I tried searching the wep and looking at some man pages, but couldn't find anything useful.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you read the FAQ here on this very site?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't know of a cross-platform solution but for linux look up opendir(), readdir().

    Here is a link that may help.

    opendir

    Jim

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Cross-platform isn't really necessary, I just thought maybe something existed.

    I'll check out opendir().

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    This works, it lists all the files in the current dir:

    PHP Code:
    #include <stdio.h>
    #include <dirent.h>

    int main()
    {
        
    DIR *dir opendir(".");
        
    struct dirent *dirent;
        
        if (
    dir)
        {
            while (
    dirent readdir(dir))
            {
                
    printf("%s\n"dirent->d_name);
            }
        }
        else
        {
            
    fprintf(stderr"error: cannot access directory\n");
        }
        
        return 
    0;

    Now I have a few questions:

    How do I get a file pointer from a dirent?

    How do I tell what type of file the dirent is, and if it's a directory, recursively go in it?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > How do I get a file pointer from a dirent?
    d_name is the file.
    If you want to do something with the file, you'll have to open it.

    > How do I tell what type of file the dirent is, and if it's a directory, recursively go in it?
    Use the stat() function (see the manual page)
    It has things like IS_DIR() and other macros to tell you what it is.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I wrote this:

    PHP Code:
    #include <stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>

    int main()
    {
        
    DIR *dir opendir(".");
        
    struct dirent *dp;
        
    struct stat stat;
        
        if (
    dir)
        {
            
    printf("\n--- %s ---\n"dp->d_name);
            if (
    stat(dp->d_name, &stat) == 0)
            {
                
    //TODO: display stats
            
    }
            else
            {
                
    fprintf(stderr"error: stat failed\n");
            }
        }
        else
        {
            
    fprintf(stderr"error: cannot access directory\n");
        }
        
        return 
    0;

    but it gives an error:

    Code:
    $ gcc test.c 
    test.c: In function ‘main’:
    test.c:14:11: error: called object ‘stat’ is not a function

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because you have a variable with the same name as the function.
    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.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Haven't noticed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort linked list by pointer manipulation
    By budala in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 03:07 PM
  2. C Bit Manipulation
    By Sicilian_10 in forum C Programming
    Replies: 4
    Last Post: 03-19-2010, 10:01 AM
  3. C Programming question about file manipulation
    By DamienCurr in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 12:00 PM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. File manipulation
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 08:07 AM