epiphany
epiphany

Reputation: 766

How to concat char array into a string

Below I have two variable store in a char array

char one[7]  = "130319";
char two[7] =  "05A501";

I try to concat them with stringstream

std::ostringstream sz;
sz << one<< two;

After that I convert it to string

std::string stringh = sz.str();

Then I try to merge it to form the path of a file and write text in that file

std::string start="d:/testingwinnet/json/";
std::string end= ".json";
std::string concat= start+stringh + end;

ofstream myfile(concat);

myfile << "test";
myfile.close();

And I am getting the following error

error C2040: 'str' : 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >' differs in levels
of indirection from 'char *

Any idea. Many thanks

Upvotes: 1

Views: 824

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409472

The problem is that you use a very old version of Visual Studio. Much much older than the C++11 standard, which introduced the ability to pass std::string as filenames to file streams.

You must use a C-style string (const char *) as filename when opening files with e.g std::ofstream.

So the solution with your current code is to do

ofstream myfile(concat.c_str());

Upvotes: 3

Michael
Michael

Reputation: 566

Consider checking out this previous answer for string concatenation How to concatenate two strings in C++?

Upvotes: 0

Related Questions