Modify input file name and print as output [C]

  • Thread starter Thread starter november1992
  • Start date Start date
  • Tags Tags
    File Input Output
Click For Summary

Discussion Overview

The discussion revolves around modifying a file name in C programming by replacing the ".txt" extension with "_out.txt". Participants explore different methods to achieve this, including string manipulation techniques and considerations for handling multiple periods in file names.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about creating an output file name by modifying the input file name, specifically replacing the ".txt" extension.
  • Another participant suggests checking for a dot in the file name and splitting the string into two parts to concatenate the new output format.
  • A different approach is proposed where characters are copied to a new string until the dot is reached, followed by appending the new suffix and extension.
  • Concerns are raised about handling file names with multiple dots, which could lead to unintended output formats.
  • Participants discuss the importance of checking for the presence of a dot in the file name before modifying it.
  • One participant mentions encountering garbage values in their output and seeks clarification on how to resolve this issue.
  • Another participant notes that initializing the character array to zero resolves the garbage value problem, leading to successful output.
  • A later reply explains the necessity of null-terminating strings for functions like strcat to work correctly.

Areas of Agreement / Disagreement

There is no consensus on a single method to modify the file name, as participants propose different approaches and express varying concerns about handling edge cases, such as multiple dots in file names.

Contextual Notes

Participants mention potential issues with garbage values and the need for proper string termination, indicating that the solutions may depend on careful memory management and string handling practices.

november1992
Messages
120
Reaction score
0
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.
 
Physics news on Phys.org
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).
 
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);

for(i=0;i<size;i++){
new = nameoffile;{
if(nameoffile == '.')
break;}

}
}
strcat(new,"_out");
strcat(new,".txt");
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:
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.
 
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);

for(i=size;i>0;i--){
if(nameoffile == '.'){
dot= i;
break;
}
}


for(i=0;i<dot;i++){
new = nameoffile;
}
printf("%s\n",out);
for(i=(size-dot);i<size;i++){
newpart = nameoffile;
}



strcat(new,"_out");
strcat(new,"newpart");
 
Last edited:
If i initialize the char array to 0, then it works perfectly. I wonder why.
 
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.
 

Similar threads

Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K