Thread: Naming an output file based on an input file name

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Naming an output file based on an input file name

    Hi,

    I have a program where I will read in a certain .txt file, such as "financial_data.txt" and I will be doing some sorting with this data.

    I want my program to write out the sorted data to an output file and I want the name of the output file to be based off of the input name. For example, since my input file is "financial_data.txt", I want the output file name to be "financial_data_out.txt".

    I am having a hard time finding examples online and in my reference manuals that will help me in writing the code necessary to do this.

    Any advice pointing me in the direction of what functions I would use to write this code would be greatly appreciated.

    Thanks,

    Mike

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You could use strrchr to find the last period in the filename. Then you know what the filename is minus the extension. Next you can then use snprintf or sprintf() to print the filename portion to a new string with _out.txt appended, like so:

    Code:
    char * sbuf;
    size_t over, sz = 10;
    
    sbuf = malloc(sizeof *sbuf * sz);
    if ((over = snprintf(sbuf, sz, "%s_out.txt", orig_file)) >= sz) {
      sbuf = realloc(sbuf, sz+over); /* should use a tmp buffer here and error check */
      snprintf(sbuf, sz, "%s_out.txt", orig_file);
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    snprintf() or sprintf() work but are a bit of overkill in this case. realloc() should be unnecessary, since the input lengths are known.

    All you're doing is inserting one string into the middle of another immediately before the last '.' (if any) in the input filename.

    All that is required is
    1) ensure a buffer (which I'll call "output file name") that is more than the sum of lengths of input filename and the string being inserted
    2) copy the input filename to the output filename
    3) find point of insertion (e.g. using strrchr() - note two 'r's),
    4) shuffle everything from the '.' to the right by the length of what is inserted (e.g. using memmove(),
    5) copy the additional string into place the space left (e.g. using memcpy())
    6) open the output file
    7) cleanup (e.g. if you created buffer to hold output file name, release it).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by grumpy View Post
    2) copy the input filename to the output filename
    3) find point of insertion (e.g. using strrchr() - note two 'r's),
    4) shuffle everything from the '.' to the right by the length of what is inserted (e.g. using memmove(),
    5) copy the additional string into place the space left (e.g. using memcpy())
    If you're going to do it this way you can replace step 4 with step 5 and just start copying at the "insertion" point and stop copying the source string. Removing the pointless need to copy characters that you are then going to memmove out of the way.

    Even with that improvement, I would still use sprintf as it's easier and probably will make the code easier to understand as well. Not to mention it's flexibility will allow you to easily change the format of the filename later if you choose to do so.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonpuz View Post
    If you're going to do it this way you can replace step 4 with step 5 and just start copying at the "insertion" point and stop copying the source string. Removing the pointless need to copy characters that you are then going to memmove out of the way.
    The trade-off with that approach is needing additional bookkeeping to keep track of what was copied and what remains to be copied.

    Quote Originally Posted by nonpuz View Post
    Even with that improvement, I would still use sprintf as it's easier and probably will make the code easier to understand as well. Not to mention it's flexibility will allow you to easily change the format of the filename later if you choose to do so.
    Having maintained code that uses both approaches, the sprintf() approach is much more difficult to maintain in the long run. Little things like the original developer adding a subtle nuance to a format string can cause all sorts of trouble to someone else who must later change that code. Yes, very convenient for original developer, but not for those who come after.

    If we were talking a C++ approach and using string streams, I'd agree. But this is a C forum, not a C++ forum.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-23-2011, 11:50 AM
  2. DLL output file naming convension
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 08-31-2007, 12:24 PM
  3. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 10:15 AM
  4. File I/O with 2 input and 1 output file
    By Sandy in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 12:06 PM