File 用Pascal读取()文件
我有一份记录,还有一份记录档案。我看不懂文件的内容。我尝试从文件中读取数据并将其保存到名为MiGuarde的记录中。当我试图打印MiGuarde时(我的文件中有数据),它什么也不显示File 用Pascal读取()文件,file,pascal,File,Pascal,我有一份记录,还有一份记录档案。我看不懂文件的内容。我尝试从文件中读取数据并将其保存到名为MiGuarde的记录中。当我试图打印MiGuarde时(我的文件中有数据),它什么也不显示 uses crt; type GUARDERIA = record nombre, direccion : string[20]; total_caniles, cantidad_caniles, nro_mascota : integer;
uses crt;
type GUARDERIA = record nombre, direccion : string[20];
total_caniles, cantidad_caniles, nro_mascota : integer;
valor_canil : real;
end;
GU = file of GUARDERIA;
var eleccion : integer; G : GU;
MiGuarde : GUARDERIA;
procedure CREAR_GUARDERIA;
begin
assign(G,'C:\Users\MACIEL\Desktop\TP3 Algoritmos\GUARDERIAS.DAT');
reset(G);
if eof(G) then begin
writeln('Ingrese el nombre de la Guarderia: ');
readln(MiGuarde.nombre);
writeln('Ingrese el total de caniles : ');
readln(MiGuarde.cantidad_caniles);
MiGuarde.nro_mascota := 0;
writeln('Ingrese el valor por dia: ');
readln(MiGuarde.valor_canil);
writeln('Ingrese la direccion: ');
readln(MiGuarde.direccion);
write(G,MiGuarde);
end
else begin
writeln('Ingrese el nuevo valor de estadia o "0" para salir');
readln(eleccion);
if eleccion > 0 then begin
MiGuarde.valor_canil := eleccion;
write(G,MiGuarde);
end;
end;
reset(G);
read(G,MiGuarde);
writeln(MiGuarde.nombre,'-',MiGuarde.cantidad_caniles);
readkey;
close(G);
end;
begin
repeat
ClrScr;
writeln('MENU');
writeln();
writeln('1. Generar guarderia (o modificar valor).');
writeln('8. Salir.');
writeln();
writeln('- ');
readln(eleccion);
case eleccion of
1 : CREAR_GUARDERIA;
8 : exit;
end;
until eleccion = 8;
readkey;
end.
您的(显然不完整的)程序有太多问题。例如,您对文件执行重置操作,然后尝试对其进行写入—这不可能成功。要进行写入,必须使用“重写”打开文件。将读写操作分离为单独的函数可能更容易/更安全
function ReadRec(n: Integer): Boolean;
begin
ReadRec := False; // assume failure
Assign(G,'data.dat');
{$I-} Reset(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
if not eof(G) then begin
Read(G,MyStore);
ReadRec := True;
end;
Close(G);
end;
function WriteRec(n: Integer): Boolean;
begin
WriteRec := False; //assume failure
Assign(G,'data.dat');
{$I-} Rewrite(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
Write(G,MyStore);
Close(G);
WriteRec := True;
end;
你确实需要更努力地学习帕斯卡语。而且,告诉我们它应该做什么会更容易些,因为我的西班牙语[?]毫无希望
您还需要组织代码,使其更易于阅读/管理
例如,您可以为读取和写入记录创建单独的函数。这将使你的工作更容易。以下示例与Freepascal/Turbo Pascal兼容。(您也没有提到您使用的是哪种Pascal编译器/方言。)我将记录保留为全局记录,以便更接近原始版本,但您也可以将其作为参数传递给这些函数
function ReadRec(n: Integer): Boolean;
begin
ReadRec := False; // assume failure
Assign(G,'data.dat');
{$I-} Reset(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
if not eof(G) then begin
Read(G,MyStore);
ReadRec := True;
end;
Close(G);
end;
function WriteRec(n: Integer): Boolean;
begin
WriteRec := False; //assume failure
Assign(G,'data.dat');
{$I-} Rewrite(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
Write(G,MyStore);
Close(G);
WriteRec := True;
end;
函数ReadRec(n:Integer):布尔值;
开始
ReadRec:=False;//假定失败
赋值(G,'data.dat');
{$I-}重置(G);{$I+}
如果结果为0,则退出;
Seek(G,n);
如果不是eof(G),则开始
Read(G,MyStore);
ReadRec:=真;
结束;
关闭(G);
结束;
函数WriteRec(n:整数):布尔值;
开始
WriteRec:=假//假定失败
赋值(G,'data.dat');
{$I-}重写(G);{$I+}
如果结果为0,则退出;
Seek(G,n);
编写(G,MyStore);
关闭(G);
WriteRec:=真;
结束;
遵循代码的逻辑有点困难。我想您正在尝试创建一个包含许多记录的数据库,而不是一条记录。因此,如果有许多记录,在更新记录时是否不需要向用户索要记录编号
下面是我的尝试,使您的代码功能性更强,可读性更强,但不改变其当前逻辑,我不确定我是否完全遵循,它似乎不完整。另外,我在谷歌上把它翻译成英语,希望能更好地理解它
至少它应该(向你)证明,书写和阅读记录确实起到了应有的作用
type
GUARDERIA = record
nombre,
direccion : string[20];
total_caniles,
cantidad_caniles,
nro_mascota : integer;
valor_canil : real;
end;
GU = file of GUARDERIA;
var
choice : integer;
G : GU;
MyStore : GUARDERIA;
////////////////////////////////////////////////////////////////////////////////
procedure PrintRec;
begin
with MyStore do begin
Writeln('Name ',nombre);
Writeln('Address ',direccion);
Writeln('Number of pets ',total_caniles);
Writeln('Cantidad caniles ',cantidad_caniles);
Writeln('Number of pets ',nro_mascota);
Writeln('Num. of stay days ',valor_canil);
end;
end;
////////////////////////////////////////////////////////////////////////////////
function ReadRec(n: Int64): Boolean;
begin
ReadRec := False; // assume failure
Assign(G,'data.dat');
{$I-} Reset(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
if not eof(G) then begin
Read(G,MyStore);
ReadRec := True;
end;
Close(G);
end;
////////////////////////////////////////////////////////////////////////////////
function WriteRec(n: Int64): Boolean;
begin
WriteRec := False; //assume failure
Assign(G,'data.dat');
{$I-} Rewrite(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
Write(G,MyStore);
Close(G);
WriteRec := True;
end;
////////////////////////////////////////////////////////////////////////////////
procedure InputRec;
begin
Write('Enter the name : ');
Readln(MyStore.nombre);
Write('Enter the number of dogs: ');
Readln(MyStore.cantidad_caniles);
MyStore.nro_mascota := 0;
Write('Enter the number of days: ');
Readln(MyStore.valor_canil);
Write('Enter the address : ');
Readln(MyStore.direccion);
end;
////////////////////////////////////////////////////////////////////////////////
procedure AddRec;
begin
if not ReadRec(0) then begin //create first record if empty file
InputRec;
WriteRec(0);
end;
PrintRec;
Writeln('Enter the stay days, or "0" to exit');
Readln(choice);
if choice > 0 then begin
MyStore.valor_canil := choice;
WriteRec(0);
end;
Writeln(MyStore.nombre,'-',MyStore.cantidad_caniles);
end;
////////////////////////////////////////////////////////////////////////////////
begin
repeat
Writeln('MENU');
Writeln;
Writeln('1. Create new record (or modify value)');
Writeln('0. Exit');
Writeln;
Writeln('- ');
Readln(choice);
case choice of
1 : AddRec;
0 : Break;
end;
until False;
Writeln('Bye');
end.
类型
GUARDERIA=记录
名义上,
direccion:string[20];
犬只总数,
坎蒂达多犬,
nro_mascota:整数;
勇敢:真实的;
结束;
GU=GUARDERIA文件;
变量
选择:整数;
G:顾;
MyStore:GUARDERIA;
////////////////////////////////////////////////////////////////////////////////
程序PrintRec;
开始
从MyStore开始
书面形式(“名称”,名称);
Writeln(“地址”,目录);
Writeln(“宠物数量”,犬只总数);
书面语(“Cantidad caniles”,Cantidad_caniles);
书面形式(“宠物数量”,nro_mascota);
书面形式(“停留天数”,valor_canil);
结束;
结束;
////////////////////////////////////////////////////////////////////////////////
函数ReadRec(n:Int64):布尔型;
开始
ReadRec:=False;//假定失败
赋值(G,'data.dat');
{$I-}重置(G);{$I+}
如果结果为0,则退出;
Seek(G,n);
如果不是eof(G),则开始
Read(G,MyStore);
ReadRec:=真;
结束;
关闭(G);
结束;
////////////////////////////////////////////////////////////////////////////////
函数WriteRec(n:Int64):布尔值;
开始
WriteRec:=假//假定失败
赋值(G,'data.dat');
{$I-}重写(G);{$I+}
如果结果为0,则退出;
Seek(G,n);
编写(G,MyStore);
关闭(G);
WriteRec:=真;
结束;
////////////////////////////////////////////////////////////////////////////////
程序输入法;
开始
写('输入名称:');
Readln(MyStore.nombre);
写('输入狗的数量:');
Readln(MyStore.cantidad_caniles);
MyStore.nro_mascota:=0;
写入('输入天数:');
Readln(MyStore.valor_canil);
写入('输入地址:');
Readln(MyStore.direccion);
结束;
////////////////////////////////////////////////////////////////////////////////
程序AddRec;
开始
如果不是ReadRec(0),则开始//如果文件为空,则创建第一条记录
InputRec;
WriteRec(0);
结束;
PrintRec;
Writeln('输入停留天数,或“0”退出');
Readln(选择);
如果选择>0,则开始
MyStore.valor_canil:=选择;
WriteRec(0);
结束;
Writeln(MyStore.nombre,'-',MyStore.cantidad_caniles);
结束;
////////////////////////////////////////////////////////////////////////////////
开始
重复
Writeln(“菜单”);
书面语;
Writeln('1.创建新记录(或修改值)';
Writeln('0.Exit');
书面语;
Writeln('-');
Readln(选择);
案例选择
1:AddRec;
0:中断;
结束;
直到错误;
书面语(“再见”);
结束。
希望这有帮助。您的(显然不完整的)程序有太多问题。例如,您对文件执行重置操作,然后尝试对其进行写入—这不可能成功。要进行写入,必须使用“重写”打开文件。将读写操作分离为单独的函数可能更容易/更安全
function ReadRec(n: Integer): Boolean;
begin
ReadRec := False; // assume failure
Assign(G,'data.dat');
{$I-} Reset(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
if not eof(G) then begin
Read(G,MyStore);
ReadRec := True;
end;
Close(G);
end;
function WriteRec(n: Integer): Boolean;
begin
WriteRec := False; //assume failure
Assign(G,'data.dat');
{$I-} Rewrite(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
Write(G,MyStore);
Close(G);
WriteRec := True;
end;
你确实需要更努力地学习帕斯卡语。而且,告诉我们它应该做什么会更容易些,因为我的西班牙语[?]毫无希望
您还需要组织代码,使其更易于阅读/管理
例如,您可以为读取和写入记录创建单独的函数。这将使你的工作更容易。以下示例与Freepascal/Turbo Pascal兼容。(您也没有提到您使用的是哪种Pascal编译器/方言。)我将记录保留为全局记录,以便更接近原始版本,但您也可以将其作为参数传递给这些函数
function ReadRec(n: Integer): Boolean;
begin
ReadRec := False; // assume failure
Assign(G,'data.dat');
{$I-} Reset(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
if not eof(G) then begin
Read(G,MyStore);
ReadRec := True;
end;
Close(G);
end;
function WriteRec(n: Integer): Boolean;
begin
WriteRec := False; //assume failure
Assign(G,'data.dat');
{$I-} Rewrite(G); {$I+}
if IOResult <> 0 then exit;
Seek(G,n);
Write(G,MyStore);
Close(G);
WriteRec := True;
end;
函数ReadRec(n:Integer):布尔值;
开始
ReadRec:=False;//假定失败
赋值(G,'data.dat');