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

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

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

Hi,

Hi! :)

Doesn't AnsiString provide a c_str() member too?

if (OpenFileDialog->Execute())
{
  std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary);
}

Hi! :)

Doesn't AnsiString provide a c_str() member too?

if (OpenFileDialog->Execute())
{
  std::ifstream infile(OpenFileDialog->FileName.c_str(), std::ios::in | std::ios::binary);
}

Hi,

Cheers, just tried that now and brings up the same error :sad:

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...

C++ string: string cpp_string; C string: string c_string; To convert C++ string to C string:

c_string = cpp_string.c_str();

To convert C string to C++ string:

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

C string: string c_string;

I always thought C style strings were null terminated character arrays and not the same as C++ strings....;)

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: [B]char[/B] c_string[]; // also need to initalize with string, or use pointer instead

I don't see a problem here. You claim

if (OpenFileDialog->Execute())
  {
   dave = OpenFileDialog->FileName;
   std::ifstream
       infile(dave.c_str(), std::ios::in | std::ios::binary);
  }

doesn't work. Well, infile doesn't take C-Strings, does it? Then you say

dave = "H:\\MyDocuments\\file";
      std::ifstream
       infile(dave.c_str(), std::ios::in | std::ios::binary);

works fine. So use the form that works:

if (OpenFileDialog->Execute())
  {
   dave = OpenFileDialog->FileName;
   std::ifstream
       infile(dave, std::ios::in | std::ios::binary);
  }

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.

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

I still don't get this line:

std::string input = OpenFileDialog->FileName.c_str();

Since OpenFileDialog->Filename is obviously a C++ string, then why use string::c_str() if you're just copying the value to another C++ string?

>>I still don't get this line:
std::string input = OpenFileDialog->FileName.c_str();

Methinks OpenFileDialog->FileName may be of type AnsiString, not of type STL C++ string. I believe AnsiString is a string class used that mimics the STL C++ string class, but it isn't the same as the STL C++ string class; and I suspect most STL C++ string class implementations don't have implicit conversions between AnsiString and STL C++ string. Therefore, the need to change AnsiString to C style string with c_str() (or whatever the equivalent function would be) method of AnsiString and then convert that C style string to STL C++ string implicitly with the assignment operator. If this is indeed the case, then my question would be why bring STL C++ string class in at all? Why not stick with AnsiString instead of STL C++ string and switch to C style string when needed using the c_str() method (or whatever the equivalent method is) of the AnsiString class?

Knowing the types of both dave and OpenFileDialog->FileName in the original post would be helpful in answering the OP question of why one of the original versions worked and the other didn't.


IMO, the following code probably doesn't work because infile is declared twice, once without the std:: scope prefix to the ifstream class outside the if statement, and once with ithe std:: scope prefix to the ifstream class within the if statement. The one inside the if statement is visible only within the if statement, since that's where it's declared. Associating the infile with a file inside the if statement doesn't associate the other infile with the same file however. Therefore, in the infile in ReadPicData isn't open and the call to ReadPicData fails. The second version of the code snippet posted has infile declared only once, outside of the if statement so it is available to ReadPicData().

ifstream infile;
 
if (OpenFileDialog->Execute())
{
    std::string input = OpenFileDialog->FileName.c_str();
    std::ifstream infile(input.c_str(), std::ios::in | std::ios::binary);
}

It seems a shame that the first parameter of ReadPicData() isn't the same type as OpenFileDialog->FileName thereby requiring one (or more) type conversions to take place, though I suspect it's something that happens relatively frequently in more complicated programs, thus requiring the programmer to be very clear about the types of each variable being used if they don't want to get caught in a guessing game.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.