簡體   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