Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading files

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
I would like to write a C program that will read all of the files in a given directory, one that will be passed in from the command line arg. I want all of the files and directorys printed to the screen. I was thinking of using the opendir function. Does anyone have any ideas on how to get this working?
 
Depends on your OS. For Unix, it is
Code:
opendir
while readdir does not return null
   display the information returned by readdir

closedir
You can also make it recursive but make sure you skip the . and .. directories otherwise you will be there forever.
 
Ok, here is what I got, now I want to print out all of the files in the directorys too. Any ideas?


Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

DIR *d;
struct dirent *dirPointer;
int count = 1;

int main( int argc, char *argv[] ) {

    d = opendir(".");
          if (argc > 1){

             while ( argc > count ){
                d = opendir(argv[count]);
                dirPointer = readdir( d );
                print();
                closedir( d );
                ++count;
                }
           }
           else {
    dirPointer = readdir( d );
    print();
    closedir( d );
}
 return 0;

}

int print(){
 if ( d == NULL ) {
        fprintf( stderr, "%s %d: opendir() failed (%s)\n",
                  strerror( errno ));
        exit( -1 );
    }
 while ( dirPointer != NULL ) {
        printf( "%s\n", dirPointer->d_name );
        dirPointer = readdir( d );
    }
return 0;
}
 
What is currently does is prints all files and floders in the current directory, unless other directorys are given as a command line argument. What I want to do is modify it so that it works the same way but insted of printing the dir it prints all the files in it and all the files in other directorys that are in that one.
 
Here is what I have worte so far, can anyone help me get it to work. The comments say what I am trying to do (the part in red).

Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

DIR *d;
struct dirent *dirPointer;
struct dirent *dirPointer2;
int count = 1;

int main( int argc, char *argv[] ) {

    d = opendir(".");

//If there are more than 1 arguments(since argv[0] is the program
//name) then it will run print() for each directory inputed.

          if (argc > 1){

             while ( argc > count ){
                d = opendir(argv[count]);
                dirPointer = readdir( d );
                print();
                closedir( d );
                ++count;
                }
           }
           else {
    dirPointer = readdir( d );
    print();
    closedir( d );
}
 return 0;

}

//print() checks to see if the directory is a valid directory
//if not an error message will be displayed.  After the check
//the file or directory will be displayed to the screen.

int print(){
 if ( d == NULL ) {
        fprintf( stderr, "%s %d: opendir() failed (%s)\n",
                  strerror( errno ));
        exit( -1 );
    }
 [COLOR=red] while ( dirPointer != NULL ) {
// check if dirPointer is a directory
//  if (dirPointer is a directory){
// dirPointer2 = readdir( d );
// while ( dirPointer2 != NULL ){
// printf( "%s/n", dirPointer2->d_name );
// } }[/color]
        printf( "%s\n", dirPointer->d_name );
        dirPointer = readdir( d );
    }
return 0;
}
 
Think about your interfaces, and use of global variables.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

void print ( char *dirname ) {
    DIR *d;
    struct dirent *dirPointer;

    d = opendir( dirname );
    if ( d == NULL ) {
        fprintf( stderr, "%s %d: opendir() failed (%s)\n",
            dirname, errno, strerror( errno ));
        exit( EXIT_FAILURE );
    }
    while ( (dirPointer=readdir(d)) != NULL ) {
        struct stat sb;
        stat( dirPointer->d_name, &sb );
        if ( S_ISDIR( sb.st_mode ) ) {
            /* do something with the directory */
            printf( "DIR FOUND: " );
            /* watch out for . and .. directories before doing this!!! */
            /* print( dirPointer->d_name ); */
        }
        printf( "%s\n", dirPointer->d_name );
    }
    closedir( d );
}

int main( int argc, char *argv[] ) {
    if ( argc > 1 ) {
        int count;
        for ( count = 1 ; count < argc ; count++ ) {
            print( argv[count] );
        }
    } else {
        print( "." );
    }
    return 0;
}

--
 
Thanks, that helped, I still have a few errors to work out. Does anyone know of a good editing program to write C that formats your code and helps you write it? I have been usinf vi and it just sucks.
 
I 've used MC's editor and joe instead of vi, but they have limitations. Emacs is nice, but it's a bit much and I don't
like lisp so there are problems from the start.
Now, and I hate to admit it, I use kwrite for a lot of
C coding from kde. It's very easy to work with and has a lot of indispensable features for coding C not usually found outside an IDE.
 
You say that vi sucks. If you mean that the syntax highlighting or formatting sucks, and you're really using vim, then you can customize it fairly heavily, though that requires reading a bunch of the manual.

If you really meant that vi sucks (that is, you don't like the cryptic commands), Emacs may be more intuitive. It also allows heavy customization of syntax highlighting and formatting. You can also select different "styles" of formatting, like K&R style and GNU style. Anything more than that will probably require using Lisp to customize.

Another editor I've used is Nedit. It aims to be more intuitive than the "standard" Unix editors while still being powerful. I only used it a long time ago, but it did support syntax highlighting and formatting.

If none of those are to your liking, I'd suggest going to and browsing through the "Text Editors" section. That should give you some options.
 
How would I ignore the . and .. directories?

Here is what I was trying and it does not work

if (dirPointer->d_name != "." ){
//print the dir
}
 
man strcmp

Probably also read something about C-style strings in general.
 
I found a way to do it, it is not the way I wanted to but it works, I don't use an if statement but instead read the dirctory 3 times, the first 2 ignore since they will be the . and .. and start printing on the 3rd one. Thanks for all the help.
 
Bad idea. Do you see any documentation telling you that [tt].[/tt] and [tt]..[/tt] are guaranteed to be the first entries read? If not, then it's not safe to assume so. Your assumption might only be true some of the time, or on certain platforms, or when your program isn't doing anything important.

You need to use the [tt]if[/tt] statement, and you need to use the [tt]strcmp[/tt] function, I mentioned before, to compare the strings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top