• Support PF! Buy your school textbooks, materials and every day products Here!

Modify input file name and print as output [C]

  • #1
120
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.
 

Answers and Replies

  • #2
chiro
Science Advisor
4,790
131
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).
 
  • #3
120
0
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:
  • #4
chiro
Science Advisor
4,790
131
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.
 
  • #5
120
0
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:
  • #6
120
0
If i initialize the char array to 0, then it works perfectly. I wonder why.
 
  • #7
chiro
Science Advisor
4,790
131
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.
 

Related Threads on Modify input file name and print as output [C]

Replies
0
Views
1K
Replies
19
Views
2K
Replies
31
Views
10K
Replies
26
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
309
  • Last Post
Replies
4
Views
7K
Replies
11
Views
3K
Replies
6
Views
3K
  • Last Post
Replies
3
Views
2K
Top