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!

need to convert STRING to CHAR * 1

Status
Not open for further replies.

MarcDePoe

Programmer
Dec 19, 2000
101
US
I need to create an output object and associate it with a string...
For instance:

string F2("\\\\Network\\path\\directory\\file.txt");
ofstream fout(F2);

I need string because I build/concat the path dynamically, but I get an error on the second line above.
(cannot convert blah blah blah to const char *)
anyone know how I can do this?

Rookie-ly,
Marc

 
char *F2;
...
char Path[]="\\\\Network\\path\\directory\\file.txt";
...
F2 = Path;


That should work.



Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
My problem is that the path is in 3 parts and I'm using the string datatype for easy concatenating...

char * num = new char[15];
//x is int
_itoa(x,num,10);
F2="\\\\Network\\path\\directory\\file_";
__ext=".txt";
F3 = F2 + num + __ext;
ofstream fout(F3);
//etc.

I was using strcat(), but I couldn't get the syntax right.

Perhaps do you know a different way of concatenating?

Marc
 
Can you us an stl string?

#include <string>

void main(){

string aString, anotherString, theFullString;

aString = &quot;/end&quot;;
anotherString = &quot;/beforeEnd&quot;;
theFullString = &quot;/beginning&quot;;

theFullString += anotherString + aString;

//theFullString = /beginning/beforeEnd/end

}

Easy? If you can't use the stl, I would use sprintf.

char totalString[ 1024 ];

sprintf(targetString, string1, string2, ..., stringn); As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Marc,

> (cannot convert blah blah blah to const char *)

LOL that's hilarious. I guess you don't think that the details of the error message would be important to help providing you a solution. You are wrong.

mbaranski,

> Can you us an stl string?

My guess is, that is exactly what he is using. That's why he put the &quot;blah blah blah&quot; in his first post because the error message with the full stl syntax didn't make any sense to him.

Anyway, if anyone is interested and my guess is correct, that means that in Marc's first post his variable 'F2' is an stl::string. So... a simple solution would be to change this:

> ofstream fout(F2);

to this:

ofstream fout(F2.c_str());


Marc,

In future posts that include code, make sure that all the variables that appear in the code have visible type declarations in your post. People need to know the 'type' to comprehend the code.

&quot;But, that's just my opinion... I could be wrong&quot;.
-pete
 
I left it out because I thought &quot;blah blah blah&quot; was French for the following:
(and I don't understand any of the code in the header file) :)

C:\C_CODE\FOUT_TEST.cpp(32) : error C2664: '__thiscall std::basic_ofstream<char,struct std::char_traits<char> >::std::basic_ofstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<c
har,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
 
> I left it out because I thought &quot;blah blah blah&quot; was French for
> the following:
> (and I don't understand any of the code in the header file) :)

LOL that was a good one Marc!

I'm glad we could solve your problem.

Happy coding
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top