Thread: Help needed with code to display something similar to DIR

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    12

    Angry Help needed with code to display something similar to DIR

    Hi, I am currently working on some code that will display all filenames, filesizes and starting inode number for the current directory (although i would like to change it so that the user can input a directory to display. Anyways here is my code...

    Code:
    #include <stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <string.h>
    
    
    int main() {
    
    
        //printf("file i-node example\n");
    
    
        DIR *currentDirectory;
        struct dirent *fileEntry;
        struct stat *file_stat;
        currentDirectory = opendir(".");
        printf("Name\tInode\tSize\n");
            if( currentDirectory != NULL ) {
                while( ( fileEntry = readdir( currentDirectory ) ) != NULL ) {
                    printf( "%s\t%d\t%u\n", fileEntry->d_name, fileEntry->d_ino, file_stat->st_size );
                }
                closedir( currentDirectory );
            }
        return 1;
    }
    The issue that I am having is that the filesizes that are given are CRAZY!, here is a copy of the output...

    Name Inode Size
    test 262624 3506801480
    coursework.c~ 265169 3506801480
    test2.c 265163 3506801480
    test2.c~ 265176 3506801480
    coursetest.c 265044 3506801480
    .. 262171 3506801480
    dir_inode.c~ 265071 3506801480
    a.out 262399 3506801480
    main.c 262408 3506801480
    dir_inode.c 265140 3506801480
    . 262217 3506801480
    coursework.c 265165 3506801480
    test.c~ 265166 3506801480
    test.c 265053 3506801480

    Thanks for reading :-)

    (I feel like it could be a stupid error that I have made as I have been staring at my screen for hours now!)
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Wouldn't it be nice to actually call stat() on the file instead of printing out data from a random memory location?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    If I knew how to do that I wouldn't be asking for advice. Thanks, ill look into that know, feel free to give a hint.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by CharlieCrawt View Post
    If I knew how to do that I wouldn't be asking for advice. Thanks, ill look into that know, feel free to give a hint.
    He gave you a big hint.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, since you're on some sort of *nix platform, I suggest you check the man page: "man 2 stat" from the command line gives lots of great info, including an example.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    thanks, im working through it now, if you cant tell i am new to programming.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    One more thing: Generally you return 0 from main when your program completes successfully. Non-zero is typically for errors or special conditions. Alternatively, you can return the standard C EXIT_FAILURE or EXIT_SUCCESS, just remember to #include <stdlib.h>.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    Ok, I'm getting really annoyed with it now, if somebody has another hint which is more obvious please let me know!

    Thanks

    Would I be looking in the right direction if i was looking into fprintf
    Last edited by CharlieCrawt; 12-05-2011 at 05:21 PM.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You've been told the answer twice! It's got nothing to with the printing, and everything to do with you not having the data you're looking for -- the size of the file!

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    struct stat *file_stat;
    Where did you set file_stat to a valid location?

    Tim S.

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    Quote Originally Posted by stahta01 View Post
    Code:
    struct stat *file_stat;
    Where did you set file_stat to a valid location?

    Tim S.
    I don't understand what you have asked, im sorry, I'm a stupid newb!
    possibly.

    if(( currentDirectory != NULL ) && (stat(argv[1],&sb) < 0)) {
    while( ( fileEntry = readdir( currentDirectory ) ) != NULL ) {
    printf( "%s\t%d\t%lld bytes\n", fileEntry->d_name, fileEntry->d_ino,(long long) sb->st_size);
    }
    closedir( currentDirectory );
    Last edited by CharlieCrawt; 12-05-2011 at 06:37 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Remember how I told you back in post #5 to type "man 2 stat" at the terminal and read the man page and example? Do that. Notice how they actually call the stat() function, like rags_to_riches said you need to do back in post #2.

    If you aren't clear on what a function call is or how to call a function in C yet, then this is probably not the best program for you to learn on. Find a simpler example of functions, then find one that includes passing parameters and checking return values. Learn how that works, then come back to this program.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    I thought that This would be easier than I am finding it as I have made a whole game in C++ which was built off of many functions.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is what I've gotten from the Pelles C help file on it:
    _stat function [not standard C]

    Purpose:
    Retrieves file status.

    Syntax:
    int _stat(const char *name, struct _stat *info);

    Declared in:
    <sys/stat.h>

    Description:
    The _stat function retrieves information about the file or directory specified in the string pointed to by name, and stores the result in a _stat structure pointed to by info. The string pointed to by name may contain a full path (from the root), a relative path (from the current directory) or just a name.

    Returns:
    Zero on success, otherwise -1 and errno contains the error code.
    What caught my eye is, it's not standard C, and I'm wondering what similar function (if any), would be standard C?

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    I am SOOOO confused ahhhh. I need to forget everything that I have tried to learn today and start over!

    Is there any chance one of you guys could complete what I am trying to do so that I can analyze it to make it work?

    Also, i get how to call a function.
    Last edited by CharlieCrawt; 12-05-2011 at 07:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe display in Code::Blocks
    By Jarobins in forum Game Programming
    Replies: 1
    Last Post: 03-04-2010, 03:15 AM
  2. Assistance greatly needed to do LCD Display
    By HELPMEPLEASE!!! in forum C Programming
    Replies: 6
    Last Post: 03-28-2009, 05:07 PM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. Display Help Needed
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 11:10 PM
  5. Need a Code to display .jpg files
    By arvindkr in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2002, 05:43 PM

Tags for this Thread