【问题标题】:How to call a string from another function如何从另一个函数调用字符串
【发布时间】:2018-11-01 17:15:44
【问题描述】:

这是一个学校作业,我们正在学习如何使用函数,我在“while”行和之后的 fscanf 行上遇到错误。

<?php

    openFile($fin);
    loadFile($fin);
	
	function openFile($fin) {
		$fin = fopen("employees.txt", "r") or die("File not found!");
	}

	function loadFile(&$fin) {
		fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
		while(!feof(&$fin)) {
			$i = $i + 1; 
			fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
		}
	}
  
?>

【问题讨论】:

  • 能否提供$emp 的值和输出错误日志。将使其更容易排除故障。

标签: php function loops while-loop scanf


【解决方案1】:

试试这个 add & openFile($fin) 并从 while(!feof(&$fin)) 中删除 &

openFile($fin);
loadFile($fin);

function openFile(&$fin) {
    $fin = fopen("demo.html", "r") or die("File not found!");
}

function loadFile(&$fin) {
    $i=0;
    fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
    while(!feof($fin)) {
        $i = $i + 1; 
        fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
    }

}

【讨论】:

    【解决方案2】:

    你应该更新

    loadFile(openFile($fin));
    
    function openFile($fin) {
        return fopen("employees.txt", "r") or die("File not found!");
    
    }
    
    function loadFile(&$fin) {
        fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
        while(!feof(&$fin)) {
            $i = $i + 1; 
            fscanf($fin, "%s%s%f", $emp[$i][0],$emp[$i][1],$emp[$i][2]);
        }
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多