I've been fiddling around with char, char* and string and thoroughly confusing myself as to what the actual differences are but all in all, I couldn't seem to figure out a way to edit a string/char/char* to do something like this automatically:

None of the string combination help I can find seems to work properly in VC++ and nothing my BASIC/C++ knowledge tells me should work, works (like the following):

int num=12;
char* cnum[3];
filename = "File" << cnum << ".txt";

I mean, in my world... that should make "File12.txt" :( xD

Anyone wanna tell me how much of a noob I am? :D! (Oh yeh, and how to do it properly)

6 Years
Discussion Span
Last Post by Cowbox

Try using a string stream:

std::ostringstream cx;

for(int i=1;i<10;i++)
  cx.str("");            // clear the stringstream
  std::string Filename=cx.str();

Obviously you have to do something with Filename but it has the basics.
Note that since I use a loop, you have to clear the stringstream on each pass.
[you can define the stringstream within the loop if you like].

Votes + Comments
The help worked, until the next error I faced :( :D
string fileName = "File";
	string extension = ".txt";
	for(char fileNum = '0'; fileNum <= '9'; ++fileNum){
	 string currentFile = fileName + string(1,fileNum) + extension;
	 cout << currentFile << endl;

Ok, well that all works as far as strings go, but after doing this and then trying to use it to open a file with something a command like this:


it just gives me an error which seems to be moaning something about chars and char*s :S



error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I tried

char* name;
ifstream myfile;

which compiles with no errors then didn't work when I tried loading the file, giving this error:

Run-Time Check Failure #3 - The variable 'name' is being used without being initialized.

Edited by Cowbox: Missing info and silly wording xD


This forum needs to make a decision on whether it's going to let me edit a post or not... <Deleted newpost due to "apparently" not being allowed to edit then it doing it anyway...>

Edited by Cowbox: n/a


This is a misleading error. Your compiler is set to unicode, hence it needs to use wide characters. std::string has a method called c_str() to convert a string to a char*. But you will need to convert it from a multi-byte-string to a wide-character-string.

(other keywords are wchar, wstring and _T("Text Here"))

Also, compiler is correct, name is not initialised and could lead to memory problems (Access violations and unexpected behaviour)


Not sure I understand what initialized means in that case :S

Surely that's what:
char* name;

And also, I set the project (in VC++) to use multibyte or not set charset and it works. - Running in unicode causes a problem with a command I'm using to rename the console window.


Ok, I fixed it with some fiddling around.

std::ostringstream cx;
int filenum=1;
std::string currentfile=cx.str();
ifstream myfile;

This works now I changed the project from the Multi-byte charset to Not set. :)
Thanks for your help guys :D

Edited by Cowbox: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.