2014-01-28 47 views
0

我想问这个小程序的一些帮助。我正在使用Embarcadero RAD XE2并尝试使用按钮和文本框来构建小型窗体。机制很简单,我在文本框中编写了一些东西,并且当我单击按钮时,我希望将这些东西写入到.txt文件中。如何使用TEdit(文本框)编写文本文件

这是形式的我.cpp文件中的代码:

//--------------------------------------------------------------------------- 
#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(); 
} 
//--------------------------------------------------------------------------- 

现在,我有这样的问题:在行 “myStr的< < A;”我得到这个错误。

[BCC32错误] STRTEST.cpp(13):E2094 '运算符< <' 不 类型 '的ofstream' 的类型的参数 '的UnicodeString' 完全解析器 上下文 STRTEST.cpp实现(10) :解析:int writefile(UnicodeString)

我不知道该怎么办。如果我用直接字符串替换A,即函数写入文件完美无瑕,并用该特定字符串写入文件。

任何人都知道如何解决这个问题?

回答

0

<<>>运营商的String值只有在项目中定义了VCL_IOSTREAM时才会执行。即使如此,它仍然无法工作,因为您使用的是ofstream,它不接受Unicode数据(String是CB2009 +中的UnicodeString的别名)。您必须改用wofstream,然后使用String::c_str()来传输数值:

//--------------------------------------------------------------------------- 
#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(); 
} 
//--------------------------------------------------------------------------- 
相关问题