| | |
AnsiString to string conversion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
Hi,
I'm having difficulty in converting an ansiString to a string, i've searched loads on this but could only find how to convert a string to an ansistring.
I'm tring to open a filestream for reading using the filename selected by the value of an open file dialog.
Code below explains
The program fails due to it not reading dave the filename correctly, I've tried loads of different ways of declaring dave, by using AnsiString as FileName returns ansiSting, but couldn't convert it back to string :rolleyes:
if i just had the code as
it works fine!!!
I'm really confused with this, can anybody offer any advise?
Thanks
I'm having difficulty in converting an ansiString to a string, i've searched loads on this but could only find how to convert a string to an ansistring.
I'm tring to open a filestream for reading using the filename selected by the value of an open file dialog.
Code below explains
C++ Syntax (Toggle Plain Text)
if (OpenFileDialog->Execute()) { dave = OpenFileDialog->FileName; std::ifstream infile(dave.c_str(), std::ios::in | std::ios::binary); }
The program fails due to it not reading dave the filename correctly, I've tried loads of different ways of declaring dave, by using AnsiString as FileName returns ansiSting, but couldn't convert it back to string :rolleyes:
if i just had the code as
C++ Syntax (Toggle Plain Text)
dave = "H:\\MyDocuments\\file"; std::ifstream infile(dave.c_str(), std::ios::in | std::ios::binary);
it works fine!!!
I'm really confused with this, can anybody offer any advise?
Thanks
Last edited by ~s.o.s~; Jan 15th, 2007 at 1:11 pm. Reason: Fixed code tags, learn to use them properly.
Hi! 
Doesn't AnsiString provide a c_str() member too?

Doesn't AnsiString provide a c_str() member too?
C++ Syntax (Toggle Plain Text)
if (OpenFileDialog->Execute()) { std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary); }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Hi!
Doesn't AnsiString provide a c_str() member too?
C++ Syntax (Toggle Plain Text)
if (OpenFileDialog->Execute()) { std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary); }
Cheers, just tried that now and brings up the same error
A good description of the problem would help you as well as the one anwering the question. What exact error does the compiler spit out ?
BTW, the variable dave is already a string variable, isn't it ? So why do you want to convert it to ANSI string again ?
Also ANSI string is the same name of the string standard class of C++ while the simple character style strings are called null terminated strings. I think you are getting confused here.
Post your relevant code with proper explanation for further help...
BTW, the variable dave is already a string variable, isn't it ? So why do you want to convert it to ANSI string again ?
Also ANSI string is the same name of the string standard class of C++ while the simple character style strings are called null terminated strings. I think you are getting confused here.
Post your relevant code with proper explanation for further help...
I don't accept change; I don't deserve to live.
C++ string:
C string:
To convert C++ string to C string:
To convert C string to C++ string:
It's as simple as that. Basically, C++ strings try to be as compatible as possible with older functions by overloading as many operators as it can to make transition seamless. However, there are some things it can't do (such as when a function expects a C string), and that's when you need to use the member function c_str() to do the conversion.
If this isn't what you wanted, post again rewording your question and tell us what errors occur when you try to compile your code.
Hope this helps
string cpp_string;C string:
string c_string;To convert C++ string to C string:
c Syntax (Toggle Plain Text)
c_string = cpp_string.c_str();
c Syntax (Toggle Plain Text)
cpp_string = c_string;
It's as simple as that. Basically, C++ strings try to be as compatible as possible with older functions by overloading as many operators as it can to make transition seamless. However, there are some things it can't do (such as when a function expects a C string), and that's when you need to use the member function c_str() to do the conversion.
If this isn't what you wanted, post again rewording your question and tell us what errors occur when you try to compile your code.
Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
Last edited by John A; Jan 15th, 2007 at 10:23 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
I don't see a problem here. You claim
doesn't work. Well,
works fine. So use the form that works:
C++ Syntax (Toggle Plain Text)
if (OpenFileDialog->Execute()) { dave = OpenFileDialog->FileName; std::ifstream infile(dave.c_str(), std::ios::in | std::ios::binary); }
infile doesn't take C-Strings, does it? Then you say C++ Syntax (Toggle Plain Text)
dave = "H:\\MyDocuments\\file"; std::ifstream infile(dave.c_str(), std::ios::in | std::ios::binary);
C++ Syntax (Toggle Plain Text)
if (OpenFileDialog->Execute()) { dave = OpenFileDialog->FileName; std::ifstream infile(dave, std::ios::in | std::ios::binary); }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
Hi,
Cheers for all your advise.
The problem was where i was declaring infile.
I had declared it outside of the if statement because i needed to refer to it outside.
I've resolved the problem now
Was so simple and i was barking up the wrong tree!!
This was my code before
This is it after, not much changed really!! took me ages to figure out the simplest of errors :rolleyes:
All's good now and i have a much clearer understanding of strings Ansistrings ect.
Cheers
Cheers for all your advise.
The problem was where i was declaring infile.
I had declared it outside of the if statement because i needed to refer to it outside.
I've resolved the problem now
Was so simple and i was barking up the wrong tree!!
This was my code before
ifstream infile; if (OpenFileDialog->Execute()) { std::string input = OpenFileDialog->FileName.c_str(); std::ifstream infile(input.c_str(), std::ios::in | std::ios::binary); } ReadPicData( infile, frame_buf, frame_size );
This is it after, not much changed really!! took me ages to figure out the simplest of errors :rolleyes:
if (OpenFileDialog->Execute()) std::string input = OpenFileDialog->FileName.c_str(); std::ifstream infile(input.c_str(), std::ios::in | std::ios::binary); ReadPicData( infile, frame_buf, frame_size );
All's good now and i have a much clearer understanding of strings Ansistrings ect.
Cheers
![]() |
Other Threads in the C++ Forum
- Previous Thread: Lookup tables - how to perform a switch using a string
- Next Thread: Need Help In Decoder Programming
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






