How do I concatenate three strings?

M

Mr. Ken

In my function, my main calls a function to save the data. In script-based
languages like
matlab, I can use multiple in strcat. However, in C++ it's not working since
first argument
of strcat must be the destination.

How can I get arround?





main{

.....
fname = "_0100_0211"; // from somewhere
save_messages(......, char fname);
.....


return (0);
}


function save_messages(......, char fname[100])
{
fname = strcat("MSG_INP_", fname, ".DAT");
fp = fopen(fname, "w");
blah blah
fclose(fp);

fname = strcat("MOD_OUT_", fname, ".DAT");
fp = fopen(fname, "w");
blah blah
fclose(fp);
.....
....

}
 
I

Ian Collins

Mr. Ken said:
In my function, my main calls a function to save the data. In script-based
languages like
matlab, I can use multiple in strcat. However, in C++ it's not working since
first argument
of strcat must be the destination.

How can I get arround?
Use std::string rather that C style "strings".

You can concatenate strings with the + operator, or by using a stringstream.
 
A

Allan M. Bruce

Mr. Ken said:
In my function, my main calls a function to save the data. In script-based
languages like
matlab, I can use multiple in strcat. However, in C++ it's not working
since
first argument
of strcat must be the destination.

How can I get arround?





main{

....
fname = "_0100_0211"; // from somewhere
save_messages(......, char fname);
....


return (0);
}


function save_messages(......, char fname[100])
{
fname = strcat("MSG_INP_", fname, ".DAT");
fp = fopen(fname, "w");
blah blah
fclose(fp);

fname = strcat("MOD_OUT_", fname, ".DAT");
fp = fopen(fname, "w");
blah blah
fclose(fp);
....
...

}

If you insist in using C style strings, have a look at sprintf() - it should
help you achieve what you want.
Allan
 
A

Allan M. Bruce

Mr. Ken said:
Thank you, I am using sprintf now.
My primary style is generic C, which I feel is more portable, and my
program
inherits
from some old work which is in C.

If I use Dev-C++ 4.9, can I use both C and C++ statements in one file? In
this way, the
old statements continue to work but I can add new ones with simpler C++
statements.

#include <string.h>
#include <string>

Yes it is possible to use C syntax within C++ programs to a large extent.
The official header for C string operations is <cstring>

Allan
 
M

Mr. Ken

Allan M. Bruce said:
If you insist in using C style strings, have a look at sprintf() - it should
help you achieve what you want.
Allan

Thank you, I am using sprintf now.
My primary style is generic C, which I feel is more portable, and my program
inherits
from some old work which is in C.

If I use Dev-C++ 4.9, can I use both C and C++ statements in one file? In
this way, the
old statements continue to work but I can add new ones with simpler C++
statements.

#include <string.h>
#include <string>
 
R

Rolf Magnus

Mr. Ken said:
If I use Dev-C++ 4.9, can I use both C and C++ statements in one file? In
this way, the old statements continue to work but I can add new ones with
simpler C++ statements.

Well, there are some differences in C and C++, so you might need some tweaks
here and there, but C++ mostly supports C as a subset.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top