简体   繁体   English

为什么ReadLn不为Delphi中的所有变量赋值?

[英]Why does ReadLn not assign values to all my variables in Delphi?

When using Delphi's ReadLn to read values from a tab-delimited file into a series of variables, why do some of the variables not get assigned to appropriate value when I step through the debugger? 当使用Delphi的ReadLn将制表符分隔的文件中的值读取为一系列变量时,为什么在逐步调试程序时某些变量没有分配为适当的值?

ie


x, y, z: Integer;
...
ReadLn(fh, x, y, z);
MessageBox(int2Str(y));
...

Only y has a value, x and z are 0 ... 只有y有一个值,x和z为0 ...

Note: This was edited after Mason Wheeler's entirely valid answer 注意:本文是在梅森·惠勒的完全有效答案之后编辑的

Readln will parse input as well as it can for the variable types you give it, but if your first one (name) is a string, it'll read everything up to the linebreak. Readln会针对输入的变量类型对输入进行尽可能多的解析,但是如果您的第一个(名称)是字符串,它将读取直到换行符为止的所有内容。 If you want to load in a tab-delimited file, I'd use a TStringList and set the delimiter character to #9. 如果要加载制表符分隔的文件,我将使用TStringList并将分隔符设置为#9。

This is a symptom of a more general gotcha, in particular trying to debug the values of unused variables. 这是更一般的陷阱的症状,尤其是尝试调试未使用的变量的值。

In Short: By default, the compiler optimises away unused variables 简而言之: 默认情况下,编译器会优化掉未使用的变量

If when writing code incrementally, you decide to debug and find out, say, that the ReadLn procedure is reading in the variables correctly, you may find that the values are empty or 0. Unless the variables are used later in the code - which may not be the case if you are debugging as you write each line - the compiler appears to optimise them away. 如果以增量方式编写代码时,您决定调试并发现ReadLn过程正在正确读取变量,则可能会发现该值为空或0。除非稍后在代码中使用了这些变量-如果您在编写每行时都在调试,则情况并非如此-编译器似乎在优化它们。

I used ReadLn in the example since it may well be that you want to use data in the second column of a csv file and so have to create various throwaway variables that are not used. 在示例中,我使用了ReadLn ,因为它很可能是您想使用csv文件第二栏中的数据,因此必须创建各种未使用的一次性变量。 When checking the values of the throwaway variables, you then find that they do not contain what you expect! 在检查一次性变量的值时,您会发现它们不包含您期望的值!

In the above example you can force the debugger to load the vlaues by simply 'using' the variables later in the code, ie 在上面的示例中,您可以通过在代码的后面“使用”变量来强制调试器加载vlaues,即


x, y, z: Integer;
...
ReadLn(fh, x, y, z);
MessageBox(int2Str(y));
MessageBox(int2Str(x));
MessageBox(int2Str(z));
...

Now, mousover will reveal the values of y and z as well 现在,mousover还将显示yz的值

The use of ReadLn() is unreliable in this example, because it will read to the first whitespace, add this to the first number, then to the second whitespace, add that, etc. You're doing a ReadLn, thus anything beyond the third integer on that line is ignored! 在此示例中,使用ReadLn()是不可靠的,因为它将读取到第一个空格,将其添加到第一个数字,然后再添加到第二个空格,添加,等等。您正在执行ReadLn,因此除该行的第三个整数将被忽略! And missing values default to zero. 缺失值默认为零。

If your numbers are prefixed with spaces, X would be the space, thus zero. 如果您的数字以空格开头,则X为空格,因此为零。 Y would be the first number up to the tab delimiter. Y将是制表符分隔符之前的第一个数字。 Z would be the second space in front of the second number, thus default zero. Z将是第二个数字前面的第二个空格,因此默认为零。

To be honest, I only use ReadLn() to read a whole string from a textfile. 老实说,我只使用ReadLn()从文本文件中读取整个字符串。 With tab-delimited types I tend to use all kinds of other techniques, including using a stringlist. 对于制表符分隔的类型,我倾向于使用各种其他技术,包括使用字符串列表。

ReadLn? ReadLn? hmmm (from memory) Was it expecting one line for each variable??? hmmm(来自内存)每个变量都期望一行吗??? Really don't remember... 真的不记得了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM