Thread: How can I save a file with a predefined extension?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    How can I save a file with a predefined extension?

    Hello everyone! I'm trying to make one complicated (for my knowledge) C program and need some help with one function. I do some stuff with a text file and I have to save the results of it in an output file which has the same name as the input file (which is not a big deal) but it has to have a ".cnt" extension. I'm not very good at programming and though I'm familiar with working with text files I can't figure out how to do this or the algorithm that does it.
    Could anyone give me some advice on this?
    Thank you preliminary!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just add ".cnt" to the filename. That's it. If the ".cnt" is suppose to replace the current extension, then remove that first. This is just string manipulation (strcat, strrchr, sprintf etc).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    So, if I got you right, I have to concatenate ".cnt" to the string that contains the name of the file with strcat? Makes sense to me. Thanks for the tip! I will try it and post again if something goes wrong.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ahp777 View Post
    Hello everyone! I'm trying to make one complicated (for my knowledge) C program and need some help with one function. I do some stuff with a text file and I have to save the results of it in an output file which has the same name as the input file (which is not a big deal) but it has to have a ".cnt" extension. I'm not very good at programming and though I'm familiar with working with text files I can't figure out how to do this or the algorithm that does it.
    Could anyone give me some advice on this?
    Thank you preliminary!
    Assuming the original filename has an extension already...

    Use strchr() to locate the dot at the beginning of the extension.
    Use strcpy() to copy the new extension over the old one.
    Open the file...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Thank you, Tater, it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Compile with *.lib
    By Kelvin in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2002, 06:24 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM