如何利用C++ Builder 调用写字本的文字并写入数据库的相应字段(有源程序翻译就可以了) (100分)

  • 主题发起人 主题发起人 KDMYDREAM
  • 开始时间 开始时间
K

KDMYDREAM

Unregistered / Unconfirmed
GUEST, unregistred user!
比如写字本名: hanzi.txt
写字本的内容是:
1 一马当先 aaa
2 四季如春 bbb
3 你好 cvc
注:1 一马当先之间有一个空格,其他一样。
数据库(SQLServer)表hanzi字段有:NO(整型),HZ(字符型),HZBM(字符型)
现在的问题是:将1,2,3送入NO字段中,一马当先,四季如春,你好送入HZ字段中,
aaa,bbb,ccc送入字段cvc中(注意是三条记录)。
主要是扫描写字本的一行,当遇到一个空格,空格前面的文字,是一个字段的内容,
比如上面有两个空格,并能实现所用写字本的数据送入数据库!不知如何实现。望告知。
希望你们能够用C++ Builder,完成此功能!或者直接翻译我的delphi程序为 C++ Builder 就可以了!
在此我提供delphi完全可以实现的源程序如下:
procedure TForm1.Button1Click(Sender: TObject);
var
TextFileVar: Textfile ;
fileN: string;
id, hz,bm,temp: string;
begin
FileN:='hanzi.txt';
AssignFile(TextFileVar,FileN);
Reset(TextFileVar);//与closeFile相对应
table1.open;
table1.edit;
while not SeekEof(TextFileVar) do
begin
Readln(TextFileVar,temp );//读写字本的一行
id:=copy(temp,1,pos(' ',temp)-1);//第一个空格前的字符串送入id中
temp:=copy(temp,pos(' ',temp)+1,length(temp)-pos(' ',temp));//文本一行去掉id以后的剩余文本字符串
hz:=copy(temp,1,pos(' ',temp)-1);
bm:=copy(temp,pos(' ',temp)+1,length(temp)-pos(' ',temp));
table1.insert;
table1.fields[0].AsInteger:=StrToInt(id);
table1.fields[1].AsString:=hz;
table1.fields[2].AsString:=bm;
end;
table1.post;
closeFile(TextFileVar);
end;
希望能够给出能完成此功能的C++ Builder程序。
 
虽然我看不太懂
我先给你个函数
它能把某一个String 根据某个字符来切分到一个TStringList
比方说source="123//1234//12345";
ch="//";
得到的TStringlist是
123
1234
12345
TStringList* TForm1::Get(AnsiString source, AnsiString ch)
{
AnsiString temp;
int i;
TStringList* result=new TStringList;
temp=source;
i=source.Pos(ch);
//得到第一个分割的标识字符串的位置
while( i!=0 )
{
result->Add(temp.SubString(0,i-1));
//copy能够将分割字符前面的字符串添加到result
temp.Delete(1,i);
//delete 将第一个分割字符前面(包括分割字符串)的字符串删除
i=temp.Pos(ch);
//重新得到i
}
result->Add(temp);
return result;
}
至于到数据库里吗,,,,好像很简单的 用TStringList ->LoadFromFile
再切分每一行的文本, 直接赋值到数据库里去咯FieldByName("字段名")->AsString=
记得给分


 
不行,我是要用C++ Bulider 完成此功能!
它从一个写字本中读入文字!
如:11 朋友 py 它们中间是空格 如何扫描写入数据库相应字段
 
TStringList * str=new TStringList();
str->LoadFromFile("你的文件名");
//假设我先将你的文件第一行导入
String ch=str->Strings[0];//这里得到这个文件的第一行的字符串
//这时候 ch应该等于"11 朋友 py" 它们中间是空格
再用我给你的那个函数
TStringList * ss=Get(ch," ");//切割字符为空格
这时候 ss是
11
朋友
py
table1.fields[0].AsInteger:=ss->String[0]
table1.fields[1].AsString:=ss->Strings[1]
table1.fields[2].AsString:=ss->Strings[2]
大致意思就是这样
具体函数名有可能出错


 
稍等片刻,我给你写完了发上来。
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str;
int iFileHandle;
int iFileLength;
int iBytesRead;
char *pszBuffer;
if (OpenDialog1->Execute())
{
try
{
iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead);
iFileLength = FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
pszBuffer = newchar[iFileLength+1];
iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);
pszBuffer[iBytesRead] = '/0';
str = pszBuffer;
delete [] pszBuffer;
FileClose(iFileHandle);
for(;;)
{
pos = str.Pos("/r/n");
if(!pos)
break;
str1 = str.SubString(1,pos-1);
str = str.SubString(pos+2,str.Length()-pos-1);
InsertIntoDataBase(str1);
}
}
catch(...)
{
Application->MessageBox("Can't perform one of the following file operations: Open, Seek, Read, Close.", "File Error", IDOK);
}
}
}
//---------------------------------------------------------------------------
void TForm1::InsertIntoDataBase(AnsiString str)
{
AnsiString str1,str2;
int pos,no;
AnsiString hz,hzbm;
str1 = str.Trim();
//下面的语句防止文本文件有空行。
if(str1 == "")
return;
pos = str1.Pos(" ");
str2 = str1.SubString(1,pos-1);
str1 = str1.SubString(pos+1,str1.Length()-pos);
no = str2.ToInt();
pos = str1.Pos(" ");
str2 = str1.SubString(1,pos-1);
str1 = str1.SubString(pos+1,str1.Length()-pos);
hz = str2;
hzbm = str1;
Query1->Parameters->ParamByName("no")->Value = no;
Query1->Parameters->ParamByName("hz")->Value = hz;
Query1->Parameters->ParamByName("hzbm")->Value = hzbm;
Query1->ExecSQL();
}
下面是Query1的SQL语句。
insert into yourtable(no,hz,hzbm) values(:no,:hz,:hzbm)
 
接受答案!
 
多人接受答案了。
 
后退
顶部