首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pascal基本读写文件处理

Pascal基本读写文件处理
EN

Stack Overflow用户
提问于 2018-05-29 14:12:02
回答 2查看 449关注 0票数 0

我目前遇到了这些错误的问题,似乎无法通过它们,我已经将我的错误以及我的代码附加在下面,谢谢。

错误:

免费Pascal编译器版本2.6.4 2014/02/26,适用于i386版权所有(c) 1993-2014目标OS: Darwin for i386编译BasicReadWrite.pas BasicReadWrite.pas(22,30)错误:不兼容的类型:获取"personArray“预期的"LongInt”BasicReadWrite.pas(25,8)错误:没有可用的默认属性BasicReadWrite.pas(25,8)致命:语法错误,";“期望但"[”发现致命:编译中止错误: /usr/local/bin/ppc386返回错误退出代码(如果未指定要编译的源文件,则正常)

代码语言:javascript
复制
program BasicReadWrite;

type

  Person = record
        name: String;
        age: String;  // Should this be an integer? Why/Why not?
  end;

 personArray = array of Person;

procedure WriteLinesToFile(var myFile: TextFile; const pe: Person);
begin
    WriteLn(myFile, pe.age);
    WriteLn(myFile, pe.name);
end;

procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
  p: Person;
begin
  setLength(p, ArrayOfPersons);
  for count:= 0 to high(ArrayOfPersons) do 
    begin
         p[count] := WriteLinesToFile();
    end;
end;

procedure ReadLinesFromFile(var myFile: TextFile);
var 
  p: Person;
  number: Integer;
  ArrayOfPersons: personArray;
begin
  for number:= 0 to 20 do 
    begin
       PrintRecords([number]);
    end;
end;

procedure Main();
var 
myFile: TextFile;
begin
  AssignFile(myFile, 'mytestfile.dat');
  ReWrite(myFile);  // Use ReWrite to open a file for writing 
  WriteLinesToFile(myFile);
  Close(myFile); // We need to close the file and re-open it, as Pascal
                // will not let you Read and write from a file at the same time.

  AssignFile(myFile, 'mytestfile.dat');
  Reset(myFile); // Open the file for reading.
  ReadLinesFromFile(myFile);
  Close(myFile);
end;

begin
  Main();
end.
EN

回答 2

Stack Overflow用户

发布于 2018-05-29 16:47:57

通常,第一个错误是首先要关注的错误。下面的错误可能只是第一个错误的后果。我将帮助您从第一个错误开始,但将其余的留给您来解决。你可能想和你的导师讨论一下这些错误。

所以,首先关注的是

BasicReadWrite.pas(22,30)错误:类型不兼容:获取"personArray“应为"LongInt”

第22行在

代码语言:javascript
复制
procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
  p: Person;
begin
  setLength(p, ArrayOfPersons); // line 22

这一行是错误的,因为:

  1. pPerson类型的记录。您不能将record.
  2. The的长度设置为第二个参数,因为SetLength()必须是integerArrayOfPersons不是整数。

我看不出有任何理由在该过程中设置任何内容的长度。

票数 3
EN

Stack Overflow用户

发布于 2018-05-30 09:29:25

以防你需要一些解释,当你声明的时候:

代码语言:javascript
复制
personArray = array of Person;

这意味着personArray是一个动态数组。首先,在使用之前,您需要指定此动态数组的长度,例如:

代码语言:javascript
复制
setlength(personArray,20);

其中20是要存储在personArray中的索引量(不要忘记第一个索引是0!)。例如:

代码语言:javascript
复制
personArray[0].name:= 'John';

年龄: String;//是否为整数?为什么/为什么不?

当然是,年龄应该是一个整数。您可以在字符串中存储年龄,但整数占用的内存较少,并且可以对整数使用数学运算(如果需要添加或减去年龄怎么办?你不能用字符串做数学运算)。仅当需要存储字母数字数据时,才应使用字符串。

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50577442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档