AnsiString to string conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 5
Reputation: elodie is an unknown quantity at this point 
Solved Threads: 0
elodie elodie is offline Offline
Newbie Poster

AnsiString to string conversion

 
0
  #1
Jan 15th, 2007
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
  1. if (OpenFileDialog->Execute())
  2. {
  3. dave = OpenFileDialog->FileName;
  4. std::ifstream
  5. infile(dave.c_str(), std::ios::in | std::ios::binary);
  6. }

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
  1. dave = "H:\\MyDocuments\\file";
  2. std::ifstream
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: AnsiString to string conversion

 
0
  #2
Jan 15th, 2007
Originally Posted by elodie View Post
Hi,
Hi!

Doesn't AnsiString provide a c_str() member too?
  1. if (OpenFileDialog->Execute())
  2. {
  3. std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary);
  4. }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: elodie is an unknown quantity at this point 
Solved Threads: 0
elodie elodie is offline Offline
Newbie Poster

Re: AnsiString to string conversion

 
0
  #3
Jan 15th, 2007
Originally Posted by Ravalon View Post
Hi!

Doesn't AnsiString provide a c_str() member too?
  1. if (OpenFileDialog->Execute())
  2. {
  3. std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary);
  4. }
Hi,

Cheers, just tried that now and brings up the same error
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: AnsiString to string conversion

 
0
  #4
Jan 15th, 2007
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...
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: AnsiString to string conversion

 
0
  #5
Jan 15th, 2007
C++ string: string cpp_string;
C string: string c_string;

To convert C++ string to C string:
  1. c_string = cpp_string.c_str();
To convert C string to C++ string:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: AnsiString to string conversion

 
0
  #6
Jan 15th, 2007
Originally Posted by joeprogrammer View Post
C string: string c_string;
I always thought C style strings were null terminated character arrays and not the same as C++ strings....
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: AnsiString to string conversion

 
0
  #7
Jan 15th, 2007
Originally Posted by ~s.o.s~ View Post
I always thought C style strings were null terminated character arrays and not the same as C++ strings....
Oh man... :o Joe is really asleep today...

Correction:
C string: char c_string[]; // also need to initalize with string, or use pointer instead
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: AnsiString to string conversion

 
0
  #8
Jan 16th, 2007
I don't see a problem here. You claim
  1. if (OpenFileDialog->Execute())
  2. {
  3. dave = OpenFileDialog->FileName;
  4. std::ifstream
  5. infile(dave.c_str(), std::ios::in | std::ios::binary);
  6. }
doesn't work. Well, infile doesn't take C-Strings, does it? Then you say
  1. dave = "H:\\MyDocuments\\file";
  2. std::ifstream
  3. infile(dave.c_str(), std::ios::in | std::ios::binary);
works fine. So use the form that works:
  1. if (OpenFileDialog->Execute())
  2. {
  3. dave = OpenFileDialog->FileName;
  4. std::ifstream
  5. infile(dave, std::ios::in | std::ios::binary);
  6. }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: AnsiString to string conversion

 
0
  #9
Jan 16th, 2007
I think it would be nice to see how dave is declared so we know what you mean by AnsiString. I'm reading that as the Borland AnsiString class, but I could be wrong and that affects the answer.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: elodie is an unknown quantity at this point 
Solved Threads: 0
elodie elodie is offline Offline
Newbie Poster

Re: AnsiString to string conversion

 
0
  #10
Jan 16th, 2007
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

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC