Question

I would like to ask for some help with this small program. I'm using Embarcadero RAD XE2 and trying to build a small form with a button and a textbox. The mechanic is simple, I write some stuff in a textbox, and I want that stuff to be written to a .txt file when I click the button.

Here is the code in my .cpp file of the form:

 //---------------------------------------------------------------------------
#include <fmx.h>
#include <iostream.h>
#include <fstream.h>
#include <String>
#pragma hdrstop
#include "STRTEST.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
int writefile(String A)
{
  ofstream myfile;
  myfile.open("D://example.txt");
  myfile << A;
  myfile.close();
  return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  String mystr = Edit1->Text+';';
  writefile(mystr);
  Form1->Close();
}
//---------------------------------------------------------------------------

NOW, I have this problem: In the line "mystr << A;" I get this error.

[BCC32 Error] STRTEST.cpp(13): E2094 'operator<<' not implemented in type 'ofstream' for arguments of type 'UnicodeString' Full parser context STRTEST.cpp(10): parsing: int writefile(UnicodeString)

I don't know exactly what to do. If I replace A with a direct string i.e (mystr << "HI"), the function writefile works flawless and writes the file with that specific string.

Anyone out there knows how to solve this?

Was it helpful?

Solution

The << and >> operators for String values are only implemented if VCL_IOSTREAM is defined in the project. Even then, it still would not work because you are using ofstream, which does not accept Unicode data (String is an alias for UnicodeString in CB2009+). You have to use wofstream instead, and then use String::c_str() to stream the value:

//---------------------------------------------------------------------------
#include <fmx.h>
#include <iostream>
#include <fstream>
#include <string>
#pragma hdrstop
#include "STRTEST.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
int writefile(System::String A)
{
    std::wofstream myfile;
    myfile.open(L"D://example.txt");
    myfile << A.c_str();
    myfile.close();
    return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    System::String mystr = Edit1->Text + L";";
    writefile(mystr);
    Close();
}
//---------------------------------------------------------------------------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top