2014-01-28 49 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(); 
} 
//--------------------------------------------------------------------------- 
相關問題