【问题标题】:Pascal Basic Read and Write File HandlingPascal 基本读写文件处理
【发布时间】:2018-05-29 06:12:02
【问题描述】:

我目前遇到了这些错误的问题,似乎无法解决它们,我已将错误和代码附在下面,谢谢。

错误:

适用于 i386 的免费 Pascal 编译器版本 2.6.4 [2014/02/26] 版权所有 (c) 1993-2014 by Florian Klaempfl 和其他人 目标操作系统:i386 的 Darwin 编译 BasicReadWrite.pas BasicReadWrite.pas(22,30) 错误: 不兼容的类型:得到“personArray”预期“LongInt” BasicReadWrite.pas(25,8) 错误:没有可用的默认属性 BasicReadWrite.pas(25,8) 致命:语法错误,“;”预期但“[” 发现致命:编译中止错误:/usr/local/bin/ppc386 返回 错误退出代码(如果您没有指定源文件是正常的 编译)

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.

【问题讨论】:

  • 您的代码有很多错误。您使用了错误的类型、错误的语法、传递的参数并不总是与声明的参数匹配等。编译器对此无能为力。例如。在 ReadLinesFromFile 中,您不会从文件中读取任何内容。在 PrintRecords 中,您实际上也不打印记录,并且使用了错误的类型,向 SetLength 传递了错误的参数,完全不清楚为什么要使用数组。给人的印象是你并不真正知道自己在做什么。请教老师或导师或任何人来帮助你。

标签: pascal


【解决方案1】:

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

所以,首先关注

BasicReadWrite.pas(22,30) 错误:不兼容的类型:得到“personArray” 预期为“LongInt”

第 22 行在

procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
  p: Person;
begin
  setLength(p, ArrayOfPersons); // line 22

那行是错误的,因为:

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

我认为没有任何理由在该过程中设置任何内容的长度。

【讨论】:

    【解决方案2】:

    以防万一您需要一些解释,当您声明时:

    personArray = array of Person;
    

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

    setlength(personArray,20);
    

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

    personArray[0].name:= 'John';
    

    年龄:字符串; // 这应该是一个整数吗?为什么/为什么不?

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2017-02-12
      • 2019-06-02
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      相关资源
      最近更新 更多