Link to home
Start Free TrialLog in
Avatar of cliff_m
cliff_m

asked on

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) {
???
}
Avatar of Wyn
Wyn

Use strrchr() and strcat();
continue...
char newap[]="text.txt";
char* pStr = strrchr(filename, '\\');
strcat(pStr,newap);
................................
char newap[]="text.txt";
char* pStr = strrchr(filename, '\\');
*(++pStr)='\0';
strcat(pStr,newap);
Avatar of cliff_m

ASKER

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
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.
if you want to have the new name (xmlfile) in the previous variable (txtfile), just use : strcat (xmlfile,txtfile);

Arnon David.
ASKER CERTIFIED SOLUTION
Avatar of Wyn
Wyn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.

Sorry,I want to comment ,reject me.
>> 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.
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.
Avatar of cliff_m

ASKER

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
Wyn,

I don't think you are following question

https://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.
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  
>> 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.