数字格式错误

发布于 2024-10-16 18:16:56 字数 1000 浏览 4 评论 0原文

所以基本上我有一个看起来像这样的记录

modulis = record
        kodas : string[4];
        pavadinimas : string[30];
        skaicius : integer;
        kiti : array[1..50] of string;
    end;

我试图从这样的文本文件中读取它:

ReadLn(f1,N);

for i := 1 to N do
    begin
        Read(f1,moduliai[i].kodas);
        Read(f1,moduliai[i].pavadinimas);
        Read(f1,moduliai[i].skaicius);
        for j := 1 to moduliai[i].skaicius do
            Read(f1,moduliai[i].kiti[j]);
        ReadLn(f1);
    end;

文件看起来像这样:

9
IF01    Programavimo ivadas 0
IF02    Diskrecioji matematika  1 IF01
IF03    Duomenu strukturos  2 IF01 IF02
IF04    Skaitmenine logika  0
IF05    Matematine logika       1 IF04
IF06    Operaciju optimizavimas 1 IF05
IF07    Algoritmu analize       2 IF03 IF06
IF08    Asemblerio kalba        1 IF03
IF09    Operacines sistemos 2 IF07 IF08

我得到 106 错误的数字格式。不知道如何解决这个问题,我不确定,但我认为这与文本文件有关,但是我从互联网复制了文本文件,所以它必须是好的:|

So basically I have a record that looks like this

modulis = record
        kodas : string[4];
        pavadinimas : string[30];
        skaicius : integer;
        kiti : array[1..50] of string;
    end;

And I'm trying to read it from the text file like this :

ReadLn(f1,N);

for i := 1 to N do
    begin
        Read(f1,moduliai[i].kodas);
        Read(f1,moduliai[i].pavadinimas);
        Read(f1,moduliai[i].skaicius);
        for j := 1 to moduliai[i].skaicius do
            Read(f1,moduliai[i].kiti[j]);
        ReadLn(f1);
    end;

And the file looks like this :

9
IF01    Programavimo ivadas 0
IF02    Diskrecioji matematika  1 IF01
IF03    Duomenu strukturos  2 IF01 IF02
IF04    Skaitmenine logika  0
IF05    Matematine logika       1 IF04
IF06    Operaciju optimizavimas 1 IF05
IF07    Algoritmu analize       2 IF03 IF06
IF08    Asemblerio kalba        1 IF03
IF09    Operacines sistemos 2 IF07 IF08

And I'm getting 106 bad numeric format. Can't figure out how to fix this, I'm not sure, but I think it has something to do with the text file, however I copied the text file from the internet so it has to be good :|

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

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

发布评论

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

评论(3

丑疤怪 2024-10-23 18:16:56

读取字符串数据与读取 Pascal 中的数字数据不同。

对于数字,Read 指令会消耗数据,直到到达空白或文件末尾。现在,这种情况下的空白可以是空格字符、制表符、EOL“字符”。因此,如果一行文本中有 2 个数字,您可以使用两个连续的 Read 来一一读取它们。

我相信你已经知道了。

我相信你认为它对字符串也有同样的作用。但事实并非如此,您不能仅通过使用两个连续的 Read 指令从一行文本中读取两个字符串值。 Read 将消耗 EOL 或 EOF 之前的所有文本。读取后,字符串变量被分配了它可以容纳的任意数量的字符,其余的数据将被丢弃。在这方面它本质上等同于ReadLn

解决方案?将输入文件中的所有数据排列在单独的行上,并最好使用 ReadLn 而不是所有 ReadLn。 (但我认为后者可能是不必要的,重新排列输入数据可能就足够了。)

或者,您需要将整行文本读入临时字符串变量,然后手动拆分它并将各部分分配给相应的记录字段,不要忘记将数值从 string 转换为 integer

您选择更适合您的。

Reading string data is different from reading numeric data in Pascal.

With numbers the Read instruction consumes data until it hits white space or the end of file. Now white space in this case can be the space character, the tab character, the EOL 'character'. So if there are 2 numbers on one line of text, you could read them one by one using two consecutive Reads.

I believe you have already known that.

And I believe you thought it would work the same with strings. But it won't, you cannot read two string values from one line of text simply by using two consecutive Read instructions. Read would consume all the text up to EOL or EOF. After the reading the string variable is assigned however many characters it can hold, the rest of the data being thrown out into oblivion. It is essentially equivalent to ReadLn in this respect.

Solution? Arrange all the data in the input file on separate lines and better use ReadLns instead of all the Reads. (But I think the latter might be unnecessary, and rearranging the input data might be enough.)

Alternatively you would need to read the whole line of text into a temporary string variable, then split it manually and assign the parts to the corresponding record fields, not forgetting also to convert the numeric values from string to integer.

You choose what suits you better.

狂之美人 2024-10-23 18:16:56

因为您已将 pavadinimas 声明为 string[30],所以无论字符串的长度是多少,它都会读取 30 个字符。例如,在下面的行中 pavadinimas 将是
“Skaitmenine logika 0” 而不是“Skaitmenine logika”

IF04    Skaitmenine logika  0

Because you have declared pavadinimas as string[30], it reads 30 character no matter what is the length of the string. For example in the following line pavadinimas will be
" Skaitmenine logika 0" instead of just "Skaitmenine logika"

IF04    Skaitmenine logika  0
堇色安年 2024-10-23 18:16:56

我不是 Pascal 程序员,但看起来文本文件中的字段不是固定长度的。您希望您的程序在读回期间如何分隔每个字段?

I'm not a Pascal programmer, but it looks like the fields within your text file are not fixed length. How would you expect your program to delimit each field during read back?

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