By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Define a string

P: 29
I need to put a filename is mixed from characters and numbers but these numbers are variable, for example;
char filename[]="xxx" then var "xxx" then var;
i.e., filename="ind23ept12 " where 23 is var x and 12 is var y.


int x;
int y;
char filename[20];
cin>>x>>y;
char *filename;
filename=strcpy(filename,"inf_h_");
filename=itoa(filename,x);
filename=strcpy(filename,"_f_");
filename=itoa(filename,y);
myfile.open("filename");
Aug 6 '07 #1
Share this Question
Share on Google+
16 Replies


P: 36
I don't know if it plays well in the C++ world, but for C you can use the sprintf function. The format would be the same as the printf function except that the first argument is the string you want to write to.
Aug 6 '07 #2

RRick
Expert 100+
P: 463
Sprintf should work in C++. Just include the c header file.

The C++ solution is to use something called an ostringstream. They are found in the <sstream> header file. You use them like cout and then extract the string info with str(). For example:

ostringstream oss;
oss << "path" << 11 << "/file" << 22.22;
cout << oss.str()

will print out "path11/file22.22"
Aug 7 '07 #3

P: 29
When I am trying to use ostringstream oss;
undecalare ostringstream
Aug 7 '07 #4

Meetee
Expert Mod 100+
P: 931
When I am trying to use ostringstream oss;
undecalare ostringstream
For that you need to add headers

#include <iostream>
#include <sstream>

Try std::ostringstream instead of ostringstream.

Regards
Aug 7 '07 #5

P: 29
Thanks it works, but i need to put this string in another variable, for instance
char filename[25];
filename=oss.str();
but the error message appears which is incompatible types in assigment of 'st::basic_string<char,.....
Aug 7 '07 #6

Meetee
Expert Mod 100+
P: 931
but the error message appears which is incompatible types in assigment of 'st::basic_string<char,.....
Read above lines again. You might have written st:: instead of std::

That may be the cause of error. Please check it.

Regards
Aug 7 '07 #7

P: 29
char filename[25];
std::ostringstream oss;
oss<<"inf_h_"<<x<<"_f_"<<y<<".txt";
filename=oss.str();
this is a part of my code,
the error message is : incompatible types in assigment of 'std::basic_string(char,std::char_traits<char>, std::allocator<char>>' to char[25]
also, i tried to use pointer to char, but error messages appears.
Aug 7 '07 #8

P: 86
Expand|Select|Wrap|Line Numbers
  1. char filename[25]
  2. filename=oss.str();
  3.  
you can't assign char arrays this way, only in construction like

char filename[25] = oss.str().c_str(); //plus you needed c_str()

but since you are using stringstreams anyway, easier would be

oss >> filename;
Aug 7 '07 #9

RRick
Expert 100+
P: 463
aeo3,

Watch how you mix these structures together. You're mixing C with C++. You can do it, but you need to be careful. One solution is to stay with C++ and use strings, and the various streams like cout and ostringstream.

If you want to use char * or char buffer[23], then you're mixing C and C++ together. You can it, but its not for the novice. For example:
Expand|Select|Wrap|Line Numbers
  1. const char * buffPtr = oss.str().c_str();
will work, but you can't modify the buffPtr values.

If you want to put the values in char buffer[23], then you will have to use the C string copy routines. For example:
Expand|Select|Wrap|Line Numbers
  1. char buffer[23];
  2. strncpy( buffer, 23, oss.str().c_str()); 
  3.  
but now buffer might not be terminated with a '\0' and you'll probably have to cast the oss c_str to (char *).

When I deal with strings, I keep them in the C++ string class and only use c_str(), when I have to pass them off to some routine.
Aug 8 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
aeo3-

Check your PM's in the top right of the page.

Thanks,

sicarie
Aug 8 '07 #11

P: 29
I think it is good idea, thanks a lot.
The only way that I found to use ostringstream is by adding std::ostringstream also, when I'm trying to define a string variable;
string var;
it is not accepted as a variable type
So, array of char is the alternative way to deal with it. But what is c.str()?.
The idea that i need to do, my program will generate a series of output files, and i need to distinguh between these files according to the value of two integer variables.
Aug 8 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
aeo3-

This is your last chance to respond to my PMs.
Aug 8 '07 #13

P: 29
to sicarie

what is the problem, you send me a message, i do not know the reasons to tell me something like that why? what happen?
Aug 8 '07 #14

sicarie
Expert Mod 2.5K+
P: 4,677
aeo3-

Go to the top right corner of the page - you will see a number followed by "PMs". Click on it, read them, and respond.
Aug 8 '07 #15

P: 29
it is zero. And I did not open my account for more than 12 hours
Aug 8 '07 #16

sicarie
Expert Mod 2.5K+
P: 4,677
it is zero. And I did not open my account for more than 12 hours
How have you confirmed receipt of them if you are not getting them? Click on the PMs area anyway, there should be 3-4 there at least, and respond to one of them.
Aug 8 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.