Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save file with new extension 1

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
DK
Hi,

I've made a program that reads a file, sorts the content and now I want to output the result to a new file. The new file should have the same name as the old, but a different extension ('.srt').
How do I do that?

Additional question: how do I open a file in a different directory? (if I just use the full path, I get an error)

Thanks in advance,
Sunaj

Here's my code so far:
----------------------------------------------------------
int main(int argc, char *argv[])
{
const char *FileName = (argc == 2) ? argv[1] : NULL;

if (FileName == NULL)
{
cerr << &quot;Usage: &quot; << argv[0] << &quot; 'filename'\n&quot;;
return 1;
}

ifstream inFile;
inFile.open(FileName, ios::in | ios::nocreate);

if (!inFile.good())
{
perror (FileName);
return 1;
}

// my manipulation is done here.

// Here I want to set NewFileName = FileName, but with new extension.

ofstream outFile(NewFileName, ios::eek:ut);
// the putput is written here.

}
 

Try this,

const char* base= strtok(FileName,&quot;.&quot;);
const char* NewFileName= strcat(base, &quot;.srt&quot;);


Anand :cool: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
hi abp

Yes I tried that, but I keep getting:
error C2664: 'strtok' : cannot convert parameter 1 from 'const char *' to 'char *'

suggestions?
sunaj
 
Declare your variable FileName as
char *FileName. Also declare base as char*,
otherwise the code is not portable. Sorry.

char* FileName=&quot;Test&quot;;
char* base = strtok(FileName,&quot;.&quot;);
char* NewFileName= strcat(base, &quot;.srt&quot;);



Anand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Hi abp,

Thanks, that works great.
Could you give me an explanation, I would like to understand...
What do you mean by 'portable'

Sunaj
 
Portable means that the code you write for
one computing platform like say Solaris, should
compile & run in another platform say Windows NT
/ IRIX without any problems.

For example most C implementations define the function
strcat() like below;

char* strcat(char *string1, const char* string2)

Hence if you write your code like below.

const char* orig=&quot;Test&quot;;
const char* str1 = strcat(orig, &quot; this code&quot;);

Compilers which doesnt do strict type checking (VC++
compilers are a good example) will allow this code to compile and run, but most UNIX C compilers will report
an error since they do much strict type checking. So if you
want your code to be portable it is better to modify
the above like this,

char* orig=&quot;Test&quot;;
char* str1 = strcat(orig, &quot; this code&quot;);

This will be acceptable to most compilers.

Anand :cool:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Hi abp,

Thanks for your help and explanation. I've got everything working now.
Just to mention it: I use a VC++ compiler and it did not accept
-------------------------------------------------------
const char* base= strtok(FileName,&quot;.&quot;);
const char* NewFileName= strcat(base, &quot;.srt&quot;);
-------------------------------------------------------

Sunaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top