To perform operations with Directory

This is a discussion on To perform operations with Directory within the C Programming forums, part of the General Programming Boards category; Dear All, I am new to Begineer in C programming..i am working for one Software Firm..my problem can be devided ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    To perform operations with Directory

    Dear All,
    I am new to Begineer in C programming..i am working for one Software Firm..my problem can be devided as follows

    1. I have one folder in any drive it contains both Source files(.C) and Header
    files(.H) i should get copy of this folder into my current working directory

    2. I should separate both .C and .H files

    3. In .C files there are preprocessor directives like #include "CAN.h" i have to convert this into lower case like #include "can.h"


    Can any body help to solve this?? Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your first problem is finding the files in a certain directory. This is covered in the FAQ.

    The next problem is to find the extension (.C and .H) in the files in the directory, and copy those to different directories based on the extension. You'll find strrchr() a useful function here.

    Copying files can be done either by reading the source file and writing the destination file, or if you are using Windows, you could use the CopyFile() function to do it for you [with the added advantage that any "hidden" information that Windows keeps about will still be copied - unlikely in this particular case, but there are files that have "extended attributes", which will not work right if they are not copied using CopyFile()].

    As for replacing the name inside #include, you have to search for #incldue [and remember that it's valid for these to be indented - the only rule is that the first non-white-space should be #, and there can be whitespace (but not newline) after # too]. You'd have to clarify if commented out lines should also be converted - if not, you have to keep track of comments and skip over until the end of the comment. After you've found the #include line, you can quite easily find the quote chars (is this ONLY for double quotes, or also angle-bracket quoted includes? - again clarify with whoever set you the task, or the technical lead or such). Here, you definitely need to copy the file to a new one. I would start by creating a temporary file, and then renaming the original file when the translation is finished, and finally renaming the new file to the original name. [You would want to keep the original file - just in case your program makes a mistake on something, so that someone can compare and fix up the code].

    This is not entirely trivial, but with some experience in file handling, it's not too difficult.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    hello mats?

    Quote Originally Posted by matsp View Post
    Your first problem is finding the files in a certain directory. This is covered in the FAQ.

    The next problem is to find the extension (.C and .H) in the files in the directory, and copy those to different directories based on the extension. You'll find strrchr() a useful function here.

    Copying files can be done either by reading the source file and writing the destination file, or if you are using Windows, you could use the CopyFile() function to do it for you [with the added advantage that any "hidden" information that Windows keeps about will still be copied - unlikely in this particular case, but there are files that have "extended attributes", which will not work right if they are not copied using CopyFile()].

    As for replacing the name inside #include, you have to search for #incldue [and remember that it's valid for these to be indented - the only rule is that the first non-white-space should be #, and there can be whitespace (but not newline) after # too]. You'd have to clarify if commented out lines should also be converted - if not, you have to keep track of comments and skip over until the end of the comment. After you've found the #include line, you can quite easily find the quote chars (is this ONLY for double quotes, or also angle-bracket quoted includes? - again clarify with whoever set you the task, or the technical lead or such). Here, you definitely need to copy the file to a new one. I would start by creating a temporary file, and then renaming the original file when the translation is finished, and finally renaming the new file to the original name. [You would want to keep the original file - just in case your program makes a mistake on something, so that someone can compare and fix up the code].

    This is not entirely trivial, but with some experience in file handling, it's not too difficult.

    --
    Mats


    hai Mats

    i got same code in Perl..i got it from other source..can you do one favour for me..i know u have well knowledge in PERL to please can u convert following code into C programming..i don't know perl i know only basics


    Code:
    use File::Spec::Functions qw/rel2abs splitpath catfile/;
    use strict;
    
    sub mmm_good {
      my $d = rel2abs($_[0]);
      mkdir $d unless -e $d;
      die unless -d $d;
      return $d;
    }
    
    my(%dirs);
    $dirs{c} = mmm_good('source');
    $dirs{h} = mmm_good('header');
    
    for my $path (map((glob $_), @ARGV) {
      next unless $path =~ /.([chCH])$/ && -r $path;
      my $ext = lc $1;
      open my $in, "<", $path;
      open my $out, ">", catfile($dirs{$ext}, (splitpath($path))[2]);
      while(my $line = <$in>) {
        $line = lc $line if /^\w*#include/;
        print $out $line;
      }
      close $out;
      close $in;
    }
    I am learning slowly PERL..thanks for help..

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    try "man perl"!
    perl can compile the above to a plain .c code also enjoy!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    try "man perl"!
    perl can compile the above to a plain .c code also enjoy!
    "plain" would be a bit excessive, I think.

    But the above code is certainly useful as a base for how to do it in C - most of it is straightforward in translating. If you don't understand the Perl, then it's not that much help, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hai Mats could you help??

    Quote Originally Posted by matsp View Post
    "plain" would be a bit excessive, I think.

    But the above code is certainly useful as a base for how to do it in C - most of it is straightforward in translating. If you don't understand the Perl, then it's not that much help, of course.

    --
    Mats
    Hai Mats
    Please can you translate above code into C programming..i am good in C programming but not in PERL...i am in urgent with this peace of code..can you give equavalent code for above
    PERL code in C programming

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    What possible motivation would there be for converting a perfectly usable (and short) perl script into a whole mass of C code?

    If you're thinking it will be a lot faster, then it won't be. The code is so heavily bound to the file system that the same constraints which affect perl (which is itself written in C) will also affect any native C program.


    > post 1 - I am new to Begineer in C programming
    > post 6 - i am good in C programming but not in PERL
    Mmm'kay, which is it?

    > i am in urgent with this peace of code..can you give equavalent code for above PERL code in C programming
    That might take the competent amongst us at least a day to write, debug and test and run to many hundreds of lines of code. It might take you with lots of help a couple of weeks to get there.


    Oh, and the . in /.([chCH])$/ should be /\.([chCH])$/ if you really only want files which end in [chCH] and not just any old filename like say file.pch
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Mats i know plain .c file produced by perl would be too BIG ... i was kidding
    the OP wants this in C because that is the requirement, to write the code in C, it just happened to find a similar code in perl.

    to OP: i do not agree with Mats here, it is not straight forward to convert the above perl code to C, too much work (as Salem said), even for experienced ones!

    actually what OP is trying to do is mostly done using some kind of script languages, C can do it, but, with a hell lot of pain in ....

    but anyways, you just need to know how to open a directory, read it, how to check if a file is directory and that should be enough!
    Last edited by manav; 04-29-2008 at 12:50 AM.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Still i am getting following error..

    Quote Originally Posted by Salem View Post
    What possible motivation would there be for converting a perfectly usable (and short) perl script into a whole mass of C code?

    If you're thinking it will be a lot faster, then it won't be. The code is so heavily bound to the file system that the same constraints which affect perl (which is itself written in C) will also affect any native C program.


    > post 1 - I am new to Begineer in C programming
    > post 6 - i am good in C programming but not in PERL
    Mmm'kay, which is it?

    > i am in urgent with this peace of code..can you give equavalent code for above PERL code in C programming
    That might take the competent amongst us at least a day to write, debug and test and run to many hundreds of lines of code. It might take you with lots of help a couple of weeks to get there.


    Oh, and the . in /.([chCH])$/ should be /\.([chCH])$/ if you really only want files which end in [chCH] and not just any old filename like say file.pch
    Global symbol "$path" requires explicit package name at Exponent.pm line 16.
    Global symbol "$path" requires explicit package name at Exponent.pm line 16.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Still i am getting following error..

    Quote Originally Posted by Salem View Post
    What possible motivation would there be for converting a perfectly usable (and short) perl script into a whole mass of C code?

    If you're thinking it will be a lot faster, then it won't be. The code is so heavily bound to the file system that the same constraints which affect perl (which is itself written in C) will also affect any native C program.


    > post 1 - I am new to Begineer in C programming
    > post 6 - i am good in C programming but not in PERL
    Mmm'kay, which is it?

    > i am in urgent with this peace of code..can you give equavalent code for above PERL code in C programming
    That might take the competent amongst us at least a day to write, debug and test and run to many hundreds of lines of code. It might take you with lots of help a couple of weeks to get there.


    Oh, and the . in /.([chCH])$/ should be /\.([chCH])$/ if you really only want files which end in [chCH] and not just any old filename like say file.pch
    Hai Salem i am getting stilll two errors? probably if you would provide C progfram means it will help me lot..thanks in advance

    Global symbol "$path" requires explicit package name at Exponent.pm line 16.
    Global symbol "$path" requires explicit package name at Exponent.pm line 16.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    That might take the competent amongst us at least a day to write, debug and test and run to many hundreds of lines of code. It might take you with lots of help a couple of weeks to get there.
    Yes, I wrote something to solve this problem - it doesn't do exactly what the perl-script does, but pretty close. [for example, it tries to create the header and source directory, and if there's already a file there by that name, it will not fail in the way the perl-script does - it will fail when it tries to create the new file in the directory].

    I'm using the Windows file-finding API, but it shouldn't be terribly hard to replace it with the corresponding Unix version with fairly minor modifications.

    Error handling in my code is there, but rudimentary.

    It took about 175 lines of code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    i am working for one Software Firm
    Sigh...cross-posted here as well.

    In short...plz email me teh codez!!!

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I thought that was a duplicate of this, rather than this thread:
    Help with File operations

    And I had already spotted the "homework" bell going off, which is why I have NOT posted my solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Well, I'm sure it's all the same, particularly this one and the one you mention...shwethu...shwetha_siddu. Certainly homework is a possibility. I like that idea better than this person already having a job (although that wouldn't really surprise me either...)

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Hai mates can u give me that code?

    Quote Originally Posted by matsp View Post
    Yes, I wrote something to solve this problem - it doesn't do exactly what the perl-script does, but pretty close. [for example, it tries to create the header and source directory, and if there's already a file there by that name, it will not fail in the way the perl-script does - it will fail when it tries to create the new file in the directory].

    I'm using the Windows file-finding API, but it shouldn't be terribly hard to replace it with the corresponding Unix version with fairly minor modifications.

    Error handling in my code is there, but rudimentary.

    It took about 175 lines of code.

    --
    Mats
    Hai Mats,
    Can you give that 175 line code..i will try to modify according to my requirement..i have tried lot..but still i am not able to meet project manager requirement..Thanks very much for your help

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. directory operations, linux
    By MK27 in forum C Programming
    Replies: 10
    Last Post: 08-01-2008, 07:15 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 08:43 AM

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