读写文件(文本文件)

greenbook 2004-11-22 10:44:04
type

cRecord3000 = Record
mNum : string;
mDate : Tdate;
mTime : Ttime;
mState : array[1..12, 1..3] of integer;
mdte : integer;
end;

有这样的一个记录,,在写进文件(文本文件) 和读出来

1。写文件,,如果写的时候文件不存在就自动创建
2。读文件,,可以,指定读哪一个记录


我用这样的方法
var
i : integer;
hfile : integer;
arecord : cRecord3000;
brecord : cRecord3000;
begin

arecord.GameNum :='ssssssdddd';
arecord.dte := 3;
for i := 1 to 12 do
begin
arecord.StakeState[i][1] :=i ;
end;

hfile:= fileopen('C:\My Documents\ddd\aaa.txt', fmOpenWrite or fmShareDenyNone);
filewrite(hfile,arecord,sizeof(cRecord3000));
fileread(hfile,brecord, sizeof(cRecord3000));
fileclose(hfile);
showmessage(brecord.mNum);
showmessage(inttostr(brecord.mdte ));
end;

写进去的是乱码,,读出来的也不是原来写进去的,,,,
而且 filewrite不能自动创建(如果文件不存在),,我也不知道什么指定读哪一条记录,,

哪位大虾帮一下忙
...全文
267 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenbook 2004-11-22
  • 打赏
  • 举报
回复
我用过 vb 里的,,open/get/put/close/可以指定一次读多长,从哪里开始读,读出来的东西可以直接放在一个结构里,,,虽然不是很好用,,但能实现我要的东西,,,,
Delphi,刚学会,,不是很熟,有哪位可以帮一下忙
greenbook 2004-11-22
  • 打赏
  • 举报
回复
,,不能用数据库,,太慢了,,,总之忘掉数据库是什么东西
jinjazz 2004-11-22
  • 打赏
  • 举报
回复
那你干吗不用数据库
greenbook 2004-11-22
  • 打赏
  • 举报
回复
bee2518(迷茫ing) 我是想有没有哪个函数可以自己直接创建,,,没有的话当然这样也可以,,


jinjazz(近身剪(N-P攻略)) 写一个字符串当然可以,,,,但是
filewrite(hfile,arecord{一个记录},sizeof(cRecord3000));这样写进去的时候会是乱码,,,

还有就是我读的时候,,,(比如一个文件时写了3000个这样的记录),,我要读出只是单单读出第一百条记录
什么读
jinjazz 2004-11-22
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then

raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount ?1 do
begin

for Y := 0 to StringGrid1.RowCount ?1 do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;

end;
bee2518 2004-11-22
  • 打赏
  • 举报
回复
不存在,就创建文件
strFile:=ExtractfilePath(application.ExeName)+'test.txt';
if not FileExists(strfile) then
begin
FileCreate(strFile);
end;
greenbook 2004-11-22
  • 打赏
  • 举报
回复
meiqingsong(阿飛) 非常感
梅青松 2004-11-22
  • 打赏
  • 举报
回复
你可以使用记录文件,
但是record中不可有不定长的类型,如string (可用char数组)

cRecord3000 = Record
mNum : array [0..50] of Char; //不可用string
mDate : Tdate;
mTime : Ttime;
mState : array[1..12, 1..3] of integer;
mdte : integer;
end;

var
MyFile: file of cRecord3000; //结构型文件
tmpRec: cRecord3000;
hFile, i: Integer;
begin
if FileExists('c:\a.txt') then //创建文件
hFile := FileOpen('c:\a.txt', fmOpenReadWrite)
else
hFile := FileCreate('c:\a.txt', fmCreate);
if (hFile < 0 ) then
exit;
try
FileSeek(hFile, 0, 0); //查找文件的开头
for i := 0 to 20 do
begin
FillChar(tmpRec, SizeOf(cRecord3000), 0);
tmpRec.mNum := 'wer erwerwer';
tmpRec.mDate := date();
tmpRec.mTime := time();
tmpRec.mdte := i;
FileWrite(hFile, tmpRec, SizeOf(cRecord3000)); //写文件
end;

FillChar(tmpRec, SizeOf(cRecord3000), 0);
//定位到第一条文件(从0开始)
if (FileSeek(hFile, 1 * SizeOf(cRecord3000), 0) >= 0) then
begin
if (FileRead(hFile, tmpRec, SizeOf(cRecord3000)) >= 0) then //读文件
ShowMessage(FormatDatetime('yyyy/mm/dd', tmpRec.mDate));
end;
finally
FileClose(hFile);
end;
end;
greenbook 2004-11-22
  • 打赏
  • 举报
回复
没有人懂吗???不可能吧

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧