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.

hello mats?

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

Hai Mats could you help??

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

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

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!

Still i am getting following error..

Originally Posted by Salem

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

Still i am getting following error..

Originally Posted by Salem

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

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.

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...)

Hai mates can u give me that code?

Originally Posted by matsp

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