How to make a new filename based on passed filename

Hello,
I am trying to take a text file and generate an XML based on it. I am tryin to take the passed filename, "C:\c_projects\test.txt", and generate "C:\c_projects\test.xml". I suppose eventually, I will want to verify that I am hacking off an extension, but for now just knowing how to replace it, assuming it's there, would be fine.
I could do this in my sleep in Perl, but I am trying to learn C++. Here is what I would do in perl ( without File::Basename):

$fname = "C:/c_projects/test.txt" ;
$fname =~ s/(\.\w+)$/\.xml/ ;

This is what I have in my C++ program:

Test::PrintXml(const char *filename) {
???
}
cliff_mAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WynCommented:
Use strrchr() and strcat();
continue...
0
WynCommented:
char newap[]="text.txt";
char* pStr = strrchr(filename, '\\');
strcat(pStr,newap);
................................
0
WynCommented:
char newap[]="text.txt";
char* pStr = strrchr(filename, '\\');
*(++pStr)='\0';
strcat(pStr,newap);
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

cliff_mAuthor Commented:
Test::PrintXml(const char *filename) {
      
char newap[]="text.txt";
char* pStr = strrchr(filename, '\\');
      *(++pStr)='\0';
      strcat(pStr,newap);

ofstream XmlFile( pStr, ios::out ) ;
--snip--

This does not work for me.
1. it creates the file in the working directory of my program.
2. I don't (won't) know the filename, just the extension.

/cliff
0
arnondCommented:
suppose you have only one '.' in the filename:

int i;
char txtfile="c:/c_projects/test.txt" ;

char xmlfile[100]; // or what ever size you want (the best is to dynamicly allocate the memory...)

for (i=0;i<100;i++)
{
   if (txtfile[i]!='.') // haven't reached the '.' yet
    xmlfile[i]=txtfile[i];
   else
    {
       strcat (txtfile+i,".xml");
       break;
    }
}

Arnon David.
0
arnondCommented:
if you want to have the new name (xmlfile) in the previous variable (txtfile), just use : strcat (xmlfile,txtfile);

Arnon David.
0
WynCommented:
cliff_m,why you reject?

If you only want to change the extension,you can simply change the letter in strrchr() you want to find.

like:
char newap[]="txt";
char* pStr = strrchr(filename, '.');
*(++pStr)='\0';
strcat(pStr,newap);

that 's you need.

>>1. it creates the file in the working directory of my program.
So,what do you want?

>>2. I don't (won't) know the filename, just the extension.
See above.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WynCommented:
>>suppose you have only one '.' in the >>filename:

arnond,you just make it more complex,why dont you simply use strrchr to find '.'  ? :-)

and you'd better search the '.' in reverse order ,i.e,from index100 to index0,that' more safe and efficient.

cliff_m,if you have problems,simply pour out before you reject.
Best Regards
Wyn.

0
WynCommented:
Sorry,I want to comment ,reject me.
0
nietodCommented:
>> reject me.
That's sounds so unkind.  :-)

Wyn's answer should work, assuming no '.' occure before the extensions, and that assumption can be eliminated.


>> char newap[]="text.txt";
>> char* pStr = strrchr(filename, '\\');
>> *(++pStr)='\0';
>> strcat(pStr,newap);
>>
>> ofstream XmlFile( pStr, ios::out ) ;
>> This does not work for me.
>> 1. it creates the file in the working directory of my program.
>> 2. I don't (won't) know the filename, just the extension.
1. it does this because you specufy "pStr" when you open the file.  You have altered the string stored in "filename"  Specifiy "filename"
ofstream XmlFile(filename, ios::out ) ;

2.  You can simply search for the last "." rather than the last "\" and change the extension.

char* pStr = strrchr(filename, '.');

However, while Wyn's solution is good, I would recommend using a string class instead.  Then you don't have to worry about the length of the string being manipulated.  Example follows.
0
nietodCommented:
string FilNam;

int PrdPos = FilNam.find_last_of('.');

if (PrdPos == basic_string::npos) // If there is no extension.
{
   PrdPos = FilNam.length();
   FilNam += '.'
};

FilNam = FilNam.substr(0,PrdPos + 1); // Remove old extension.
FilNam += "???"; // Add new extension.
0
cliff_mAuthor Commented:
I am sorry for rejecting Wyn answer. I don't want to be an ungrateful A**. It was very likely an error on my part. It was late, and I thought that I had tried the variations suggested like searching for '.' and also opening filename instead of pstr. I will try them out again and award the points to Wyn.

:^)

Cliff
0
nietodCommented:
Wyn,

I don't think you are following question

http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10261900 

anymore--and I don't blame you.  But you've been asked to answer, which is fair, as you really were first.
0
WynCommented:
nietod , I'm a very young student.I lack of many insight and knowledge.For example,I got a deeper understanding from you though the question of hiding and minimizing a dialogue.I think you still have an impression,right?

So,every time I check the answer button it seems to be a challenge for me.But I still believe ,no matter how sharp an expert is,like you,at the very beginning,still an idiot but gradually became an expert.Just follow your way:)
Thus blame is good for me always,let alone where is it?
Thanx!
My best regards to all here
BTW:sorry for my bad English:(
Wyn  
0
nietodCommented:
>> I think you still have an impression,right?
I don't know what you mean by that.

>> no matter how sharp an expert is,like
>> you,at the very beginning,still an idiot
And still an idiot at the end.  I probably learn 10 new things a day on this site alone.  If you participate here, you will learn a lot.  You also will make mistakes alot.  Everyone does!  (And I make more than most vecause I answer more than most.)  If you are reasonably sure you are right, submit an answer.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.