繁体   English   中英

如何递增变量,以便它在While循环中从文本文件中选择不同的行

[英]How do I increment a variable so it chooses differnet lines from a text files in a While Loop

我有一个正在写的脚本。 这是怎么回事。 有一个while循环。 在while循环中,变量是X的常量。我如何在while循环的每个循环中从第一行,第二行等更改X并从.txt文件中提取X。 一切都扎根。 谢谢

$f = fopen("some.txt", "r");
while (!feof($f) && $some_condition) {
    $x = fgets($f);
    // do something
}
fclose($f);

这样就足够了吗?

这是伪代码上尉柯克:

//we assume current working directory is root
fileHandle = openFile("Read","some.txt");
X = pull("X",fileHandle);

while( X is constant )
{
  XFactor = factor(X);
}

我可以使用有关您来自什么Universe,您打算使用的编程语言以及您要发生的事情的更多详细信息的更多详细信息来完善和改进此功能。

//get the lines of the file into an array
$file_array = file($file_name);

//go through the array line by line
foreach ($file_array as $line_number => $line)
{
  //you didn't tell us what you are doing with each line
  //so you will need to change this to your liking
  $X = $line; // Handle the line
}

编辑:请注意,对于非常大的文件,这可能不是一个好方法,因为这会将整个文件一次加载到内存中。

暂无
暂无

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

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