Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: ifstream fmx seatle os x Yoshmite



Permlink Replies: 2 - Last Post: Jun 22, 2016 5:19 PM Last Post By: Richard Walborn
Richard Walborn

Posts: 15
Registered: 10/28/11
ifstream fmx seatle os x Yoshmite
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 9:41 PM
I can't seem to get istream to work on OS X

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;
ifstream presetFile( FileName.c_str());

presetFile return a NULL

However

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;

presetFile= new TFileStream(FileName.c_str(), fmOpenRead);

Returns a valid stream.
However
if( SaveDialog1->Execute())
{
FileName =SaveDialog1->FileName;
ofstream presetData(FileName.c_str(),ios::out);
Works fine.

Anyone have any ides.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ifstream fmx seatle os x Yoshmite
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 22, 2016 10:09 AM   in response to: Richard Walborn in response to: Richard Walborn
Richard wrote:

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;
ifstream presetFile( FileName.c_str());

What is the actual value of OpenDialog1->FileName before assigned to the
AnsiString? Does it contain any non-ASCII characters in it? If so, OSX
filenames are stored in UTF-8, but Unicode strings are expected to be in
fully decomposed form (similar to Unicode Normalization Form NFD) prior to
being encoded as UTF-8. Using AnsiString does not take that into account.
Try using UTF8String instead.

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;
presetFile= new TFileStream(FileName.c_str(), fmOpenRead);

OpenDialog1->FileName returns a UnicodeString, and TFileStream takes a UnicodeString,
but you are assigning the first UnicodeString to an AnsiString variable and
then assigning its char* to the second UnicodeString, so there are two data
conversions being performed. Depending on the encoding of the AnsiString
and the actual characters involved, that conversion may be either lossy or
loss-less. I'm guessing it is loss-less in your example. The RTL handles
the encoding of UnicodeString to UTF-8 when calling into the underlying OS
functions.

if( SaveDialog1->Execute())
{
FileName =SaveDialog1->FileName;
ofstream presetData(FileName.c_str(),ios::out);

Same thing as with ifstream. What is the actual value of SaveDialog1->FileName
prior to being assigned to AnsiString? If it has non-ASCII characters in
it, try using UTF8String instead.

--
Remy Lebeau (TeamB)
Richard Walborn

Posts: 15
Registered: 10/28/11
Re: ifstream fmx seatle os x Yoshmite
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 22, 2016 2:04 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy,

Using UTF8String did not help.

The exact file name including path "/Users/richwalborn/Documents/BLUESKY AMC PRESET. TXT"

there are no strange characters.

Rich

Remy Lebeau (TeamB) wrote:
Richard wrote:

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;
ifstream presetFile( FileName.c_str());

What is the actual value of OpenDialog1->FileName before assigned to the
AnsiString? Does it contain any non-ASCII characters in it? If so, OSX
filenames are stored in UTF-8, but Unicode strings are expected to be in
fully decomposed form (similar to Unicode Normalization Form NFD) prior to
being encoded as UTF-8. Using AnsiString does not take that into account.
Try using UTF8String instead.

AnsiString FileName;
if( OpenDialog1->Execute() )
{
FileName= OpenDialog1->FileName;
presetFile= new TFileStream(FileName.c_str(), fmOpenRead);

OpenDialog1->FileName returns a UnicodeString, and TFileStream takes a UnicodeString,
but you are assigning the first UnicodeString to an AnsiString variable and
then assigning its char* to the second UnicodeString, so there are two data
conversions being performed. Depending on the encoding of the AnsiString
and the actual characters involved, that conversion may be either lossy or
loss-less. I'm guessing it is loss-less in your example. The RTL handles
the encoding of UnicodeString to UTF-8 when calling into the underlying OS
functions.

if( SaveDialog1->Execute())
{
FileName =SaveDialog1->FileName;
ofstream presetData(FileName.c_str(),ios::out);

Same thing as with ifstream. What is the actual value of SaveDialog1->FileName
prior to being assigned to AnsiString? If it has non-ASCII characters in
it, try using UTF8String instead.

--
Remy Lebeau (TeamB)

Edited by: Richard Walborn on Jun 22, 2016 5:18 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02