File Handling in C

This is a discussion on File Handling in C within the C Programming forums, part of the General Programming Boards category; Is there an easy way in C to combine multiple files into one file? The filenames will be generated by ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    File Handling in C

    Is there an easy way in C to combine multiple files into one file? The filenames will be generated by another tool and will be unique. I am current accomplishing this task by manually inputting the unique filenames into a file and using that file's contents as the files that should be opened and copied to a new destination file. Is there any way to have the program to automatically copy all files in a particular directory or all files with a particular extension without me manually typing these filenames into another file?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    How about passing all the files to merge on the command line...

    Like
    mymerge *.txt
    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.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Could you elaborate a little more? I'm not sure I follow what you are saying...Thanks.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Could you elaborate a little more? I'm not sure I follow what you are saying

    Unexpected results using argc / *argv[]?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >Could you elaborate a little more?
    When you call the program from the command line, your shell uses wildcards to grab files with similar names. In Salem's case, the command
    Code:
    mymerge *.txt
    Uses the * wildcard, which should grab every file in the current directory that ends with .txt. It then passes all of those files to your program instead of just one.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Thanks! That was a big help. I was able to use the code from the other post you specified!

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Salem
    How about passing all the files to merge on the command line...

    Like
    mymerge *.txt
    This technique only works in *nix systems. On Windows and DOS you have to use the functions for file traversal.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > On Windows and DOS you have to use the functions for file traversal.
    Only if your crummy compiler doesn't include a library to expand argv[] for you.
    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.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Salem
    > On Windows and DOS you have to use the functions for file traversal.
    Only if your crummy compiler doesn't include a library to expand argv[] for you.
    Didn't realize compilers did this. Which ones? And how do you pass a '*' without the expansion? I got bit on that poring to a Unix system and had to rewrite a lot of code. Yech!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    In the unix world, wildcard expansion is a feature of the shell, and it expands wildcards before spawning the process with the expanded argv[]
    In the MS world (DOS/Win), the shell doesn't do this, so an optional library could be linked into the startup code to achieve the same thing.

    The archaic MS compilers (probably others of that era as well) usually had some optional library called setargv
    http://forums.devshed.com/t90462/s.html

    gcc based compilers ported to windows usually expand arguments for you, basically for transparency with the unix world from which they came.



    If you didn't link with this library, then
    myprog *
    got argv[1] = "*"

    If you did link with this library, then
    myprog *
    did the usual unix thing of expanding wildcards

    > And how do you pass a '*' without the expansion?
    In what - DOS / Win / Unix?
    This is a feature of whatever shell you're using
    In BASH for instance, you would do this
    Code:
    echo *
    set -o noglob
    echo *
    Assuming that glob was not turned off, the first echo lists all the filename in the current directory, and the 2nd just prints *
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 12:43 PM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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