Thread: Program or Script to Add Header to .c and .h files.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Program or Script to Add Header to .c and .h files.

    Hi... i was going to make myself a program to add headers to .c and .h files, but as i dont want to reinvent the wheel, is there such program on the intertet? As header i mean, a comment with the name of the author, version, date of creation, licesence, and so on.

    Thank you!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Sink0 View Post
    Hi... i was going to make myself a program to add headers to .c and .h files, but as i dont want to reinvent the wheel, is there such program on the intertet? As header i mean, a comment with the name of the author, version, date of creation, licesence, and so on.

    Thank you!
    Pretty much all IDEs I know of support this feature... So use an IDE, and find out for that specific IDE.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sink0 View Post
    Hi... i was going to make myself a program to add headers to .c and .h files, but as i dont want to reinvent the wheel, is there such program on the intertet? As header i mean, a comment with the name of the author, version, date of creation, licesence, and so on.

    Thank you!
    Considering that it's a real simple task... it might be a fun little exercise to write...

    But, yes, most IDEs do support "code snippets" you can use to insert the headers at creation time.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Hmm i was wondering .. how can i "insert" lines on the begin of a file? If i write on a file from the begin it will start overwriting what is already on the file...

    Thank you!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You write the header to a new file, then the full contents of the .c or .h file to that new file, then rename it. Or, if you use Linux:
    Code:
    $ cat header.txt foo.c >foo.tmp && mv foo.{tmp,c}

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sink0 View Post
    Hmm i was wondering .. how can i "insert" lines on the begin of a file? If i write on a file from the begin it will start overwriting what is already on the file...

    Thank you!
    Open the header file
    Read it into Memory
    Open the target file
    Write the header
    Open the source file (c or h)
    read it into memory
    write it to the target file
    close files
    delete source file
    rename target file

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I was wondering if that would not make use of too much memory, but i guess there is no option. Thank you!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well you don't have to keep everything in memory for huge files. Just re-read the portions if you must.

  9. #9
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Would be a simple script in Perl or Awk. I may do something like this~ pretty good idea.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Well you don't have to keep everything in memory for huge files. Just re-read the portions if you must.
    Yep... read and write the source files as binary in 1k chunks if you like, reusing the same buffer.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by \007 View Post
    Would be a simple script in Perl or Awk. I may do something like this~ pretty good idea.
    It's pretty simple in C too ....

    Remember, the forum is about C code...

  12. #12
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I know, but the topic creator asked for a program or script that did this. A C program isn't a script, but if you were to script it something like Perl or Awk would be nice. On the other hand a program may be in C. I haven't heard of a program that does this, but it's interesting.
    Last edited by \007; 01-11-2011 at 08:40 AM.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A shell script could do this too.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    True, but less portable than something like Perl at least to me. If portability wasn't a concern then shell would be the best solution I think.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I am slowly programing here haha. Its my mother's birthday today, so no time to work. Just one advice. I am going to make a GUI for the program (i am going to try as i dont have any experince with windows forms with no Visual Studio hehe). First, how can i make the program browse the file system to find the file i want to add the header? And second, if i use some sort of scanf for the input, how can i chose the max size of the input string. I could use malloc, but i dont the size to malloc haha. I can malloc a super big size.. but that would destroy my heap...

    Hmmm maybe i am going to make use of C++.. not sure...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files in .h
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 08:58 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. Difference between .h and .c files
    By Metalix in forum C Programming
    Replies: 9
    Last Post: 01-19-2005, 08:38 PM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM