如何使用TEdit(文本框)中的文本编写文件
How to write a File with text from a TEdit(Textbox)
我想寻求一些关于这个小程序的帮助。我正在使用Embarcadero RAD XE2,并试图构建一个带有按钮和文本框的小表单。机制很简单,我在一个文本框中写一些东西,我希望当我点击按钮时,这些东西被写到一个.txt文件中。
这是我的.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错误]STREST.cpp(13):E2094'运算符<lt;'未在中实现"UnicodeString"类型的参数的类型为"ofstream"完整分析器上下文STREST.cpp(10):解析:int writefile(UnicodeString)
我不知道该怎么办。如果我用一个直接字符串(即mystr<<"HI")替换A,函数writefile会完美地工作,并用该特定字符串写入文件。
有人知道怎么解决这个问题吗?
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();
}
//---------------------------------------------------------------------------
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入