pls help in writing a program

This is a discussion on pls help in writing a program within the C++ Programming forums, part of the General Programming Boards category; Hi, Pls help.Iam trying to write a program that searches the my hard drive to find a .cpp and reads ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Smile pls help in writing a program

    Hi,


    Pls help.Iam trying to write a program that searches the my hard drive to find a .cpp and reads the lines in the .cpp file

    For example if user enters the string like c:bsb77/programs it has to find the .cpp files and read the file and count the lines in the cpp file.

    Is there any standard fuctions that i can use for searching the hard dribve for a string .


    Thanks
    BSB77

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    http://www.cprogramming.com/cboard/s...ghlight=walker
    or
    http://www.cprogramming.com/cboard/s...ghlight=walker

    Travsersing a directory tree is OS/compiler specific

    Searching for a fixed string in a file is standard - use the strstr function

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Thanks Salem !

    Thanks Salem ! I will check the URLS !

    bsb77

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Salem can you pls help !

    I just looked at the program.Iam running this on UNIX>when i compiled my program iam getting many errors like dir.h not found.

    struct ffblk finder not found ... can you pls tell me whether the walker is good enough to use in UNIX ?

    Thanks
    bsb77


    This is what i have as my program :

    #include <iostream.h>
    #include <dir.h>
    #include <string.h>

    #define ALL_ATTS ( \
    FA_DIREC | \
    FA_ARCH )

    void walker ( char *path ) {
    struct ffblk finder;
    unsigned int res;

    for ( res = findfirst ( "*.*", &finder, ALL_ATTS );
    res == 0;
    res = findnext( &finder ) ) {
    if ( strcmp(finder.ff_name, ".") == 0 ) continue; // current dir
    if ( strcmp(finder.ff_name, "..") == 0 ) continue; // parent dir

    // if its a directory, examine it
    // else compare the filename with the one we're looking for
    if ( finder.ff_attrib & FA_DIREC ) {
    char newpath[MAXPATH];
    strcpy( newpath, path );
    strcat( newpath, "\\" );
    strcat( newpath, finder.ff_name);
    chdir( finder.ff_name );
    walker( newpath );
    chdir( ".." );
    } else {
    if ( strcmp( finder.ff_name, "words.txt" ) == 0 ) {
    printf( "Found!!!" );
    }
    }
    }
    }

    int main ( ) {
    char *root = "\\";
    chdir( root );
    walker( root );
    return 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > I just looked at the program.Iam running this on UNIX
    Then you need the opendir / readdir / closedir functions, and the stat() function (in sys/stat.h) to determine whether the current name is a file or a directory.

    This example is for M$ operating systems

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. Writing a checklist program for MTGO?
    By CrYpTiC in forum Game Programming
    Replies: 2
    Last Post: 04-08-2005, 09:46 PM
  3. Help on writing program
    By robasc in forum C Programming
    Replies: 18
    Last Post: 03-07-2005, 11:19 AM
  4. writing a program called tail like the one in unix
    By fanaonc in forum C Programming
    Replies: 2
    Last Post: 11-04-2001, 01:20 AM

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