1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Modify input file name and print as output [C]

  1. Sep 14, 2012 #1
    I have a question, how can I make an output file that takes the name of the input file and replaces the .txt extension with "_out.txt"?

    For example, an input file with the name “new.txt” would result in an output file named new_out.txt”.

    my friend told me to use strcat, but that just adds to an existing string.
  2. jcsd
  3. Sep 14, 2012 #2


    User Avatar
    Science Advisor

    Hey november1992.

    I would take your friends advice.

    Basically you are given a file name (either it's passed explicitly or its calculated say from doing a directory search or something similar) and then what you do is is check if there is a separator (i.e a dot '.') at the end of the file (from the end going backwards) and then take that part of the string (from the start up to but not including the dot) and add "_new" and then append whatever was after the dot from the first string.

    So basically you have a file: divide it into two strings (1 is before the last separator, 2 is the rest of the string), create a new string where you concatenate 1 + "_new" + 2 and that's your output.

    With a decent string library it's very easy to concatenate strings together (like strcat).
  4. Sep 14, 2012 #3
    What if instead of dividing the string into two parts, I just copy the characters to another string until I reach the '.' character, and then I just append the remaining parts?

    size = strlen(nameoffile);

    new = nameoffile;{
    if(nameoffile == '.')

    Edit: Garbage is being stored inbetween the '.' and the strings i added. http://i.imgur.com/VCygH.png

    I noticed there are two periods, so I just moved the if statement before I set new equal to nameoffile. I still have the garbage though.
    Last edited: Sep 14, 2012
  5. Sep 14, 2012 #4


    User Avatar
    Science Advisor

    You can do that but the reason I gave a more complex form is if your file (for some reason) has many dots '.' in the file instead of just one. If it has many dot's then you will insert your "_new" after the first dot and then you will have something like "this_new.file.txt" which is probably not what you intended.

    The other thing that you want to check is whether you have a file that even has a type separator (i.e. has no dot at all) and then either just add a text extension to it or generate a warning or an error.

    If your file is gauranteed to be in the right format, then you could do the above as well.

    Basically though, if you have routines that take a subset of a string (with known start and end points) and a routine that searches for a character in a string and returns it's position (if it exists), then doing the routine I suggested should only take roughly 10 lines if that.
  6. Sep 14, 2012 #5
    I think my teacher might put extra periods, so I'll go with what you said.
    I get garbage though, I'm not sure how to fix this. http://i.imgur.com/W1dAd.png
    Nevermind i haven't figured it out. Here is the code i wrote:

    size = strlen(nameoffile);

    if(nameoffile == '.'){
    dot= i;

    new = nameoffile;
    newpart = nameoffile;

    Last edited: Sep 15, 2012
  7. Sep 15, 2012 #6
    If i initialize the char array to 0, then it works perfectly. I wonder why.
  8. Sep 15, 2012 #7


    User Avatar
    Science Advisor

    The string routines like strcat require null terminated strings: basically this means that the strings need to have a 0x0 character at the end to denote the end of the string.

    You need to set the character after the last one denoting the actual string contents to zero: so if your string is n characters long, the n+1 cell must equal 0x0.

    This explains why zeroing the memory makes it work.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook