读取 Pascal 中的整数

发布于 2024-08-20 03:54:33 字数 250 浏览 5 评论 0原文

我用的是帕斯卡。我在处理读取文件时遇到问题。

我有一个包含整数的文件。我读取文件的帕斯卡是:

read(input, arr[i]);

如果我的文件内容是 1 2 3 那么它很好,但如果它是 1 2 31 2 3(在此处输入) (末尾有一个空格或空行)那么我的 arr 将是 1 2 3 0

I'm using Pascal. I have a problem when dealing with reading file.

I have a file with integer numbers. My pascal to read the file is:

read(input, arr[i]);

if my file content is 1 2 3 then it's good but if it is 1 2 3 or 1 2 3(enter here) (there is a space or empty line at the end) then my arr will be 1 2 3 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缘字诀 2024-08-27 03:54:33

您可以将字符串读入临时文件,然后在转换之前修剪它。

提及诸如您正在使用什么平台上的 Pascal 类型之类的基础知识并没有什么坏处,以便人们可以给出具体的答案(正如文章所指出的,在许多 Pascal 中没有一个很好的 OOTB 方式)

You could read the string into a temporary and then trim it prior to converting it.

It doesnt hurt to mention basics like what type of Pascal on what platform you're using in order that people can give a specific answer (as the article notes, there isnt a nice way OOTB in many Pascals)

动听の歌 2024-08-27 03:54:33

据我所知,read字面上将文件读取为字符流,其中有空格和回车符,但我相信当您读入整数数组时,这些应该被忽略。您的文件实际上在每个数字之间包含空格字符吗?

另一种方法是使用 readLn 并将所需的整数存储为文件中的新行,例如

1

2

3

From what I can recall read literally reads the file as a stream of characters, of which a blank space and carriage return are, but I believe these should be ignored as you are reading into an integer array. Does your file actually contain a space character between each number?

Another approach would be to use readLn and have the required integers stored as new lines in the file, e.g.

1

2

3

要走干脆点 2024-08-27 03:54:33

我已经在 Delphi 2009 控制台应用程序上测试了该问题。这样的代码的

var
  F: Text;
  A: array[0..99] of Integer;
  I, J: Integer;

begin
  Assign(F, 'test.txt');
  Reset(F);
  I:= -1;
  while not EOF(F) do begin
    Inc(I);
    Read(F, A[I]);
  end;
  for J:= 0 to I do write(A[J], ' ');
  Close(F);
  writeln;
  readln;
end.

工作原理与您编写的完全一样。可以使用跳过所有空白字符的 SeekEOLN 函数来改进;下一个代码不会产生错误的附加零:

var
  F: Text;
  A: array[0..99] of Integer;
  I, J: Integer;

begin
  Assign(F, 'test.txt');
  Reset(F);
  I:= -1;
  while not EOF(F) do begin
    if not SeekEOLN(F) then begin
      Inc(I);
      Read(F, A[I]);
    end
    else Readln(F);
  end;
  for J:= 0 to I do write(A[J], ' ');
  Close(F);
  writeln;
  readln;
end.

由于所有这些工作人员只是 Delphi 中的遗产,我认为它必须在 Turbo Pascal 中工作。

I have tested the problem on Delphi 2009 console applications. Code like this

var
  F: Text;
  A: array[0..99] of Integer;
  I, J: Integer;

begin
  Assign(F, 'test.txt');
  Reset(F);
  I:= -1;
  while not EOF(F) do begin
    Inc(I);
    Read(F, A[I]);
  end;
  for J:= 0 to I do write(A[J], ' ');
  Close(F);
  writeln;
  readln;
end.

works exactly as you have written. It can be improved using SeekEOLN function that skips all whitespace characters; the next code does not produce wrong additional zero:

var
  F: Text;
  A: array[0..99] of Integer;
  I, J: Integer;

begin
  Assign(F, 'test.txt');
  Reset(F);
  I:= -1;
  while not EOF(F) do begin
    if not SeekEOLN(F) then begin
      Inc(I);
      Read(F, A[I]);
    end
    else Readln(F);
  end;
  for J:= 0 to I do write(A[J], ' ');
  Close(F);
  writeln;
  readln;
end.

Since all that staff is just a legacy in Delphi, I think it must work in Turbo Pascal.

病女 2024-08-27 03:54:33

如果我记得有一个名为 Val 的字符串函数,它将字符串转换为数字......我对 Pascal 的了解有点生疏(Turbo Pascal v6)

var
   num : integer;
   str : string;
begin
   str := '1234';
   Val(str, num); (* This is the line I am not sure of *)
end;

希望这会有所帮助,
此致,
汤姆.

If I recall there was a string function called Val that converts a string to a number...my knowledge of Pascal is a bit rusty (Turbo Pascal v6)

var
   num : integer;
   str : string;
begin
   str := '1234';
   Val(str, num); (* This is the line I am not sure of *)
end;

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文