Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从txt文件中加载一个数字并将其用作对象_Php_String_Object - Fatal编程技术网

Php 从txt文件中加载一个数字并将其用作对象

Php 从txt文件中加载一个数字并将其用作对象,php,string,object,Php,String,Object,我想加载一个包含数字的文件,并将其用作数字(而不是字符串)。有什么解决办法吗?我得到的错误 对非对象调用成员函数seek() 我的PHP代码 $in = fopen('emails.txt','r'); $out = fopen('currentposition.txt', 'r+'); $pos = file_get_contents('currentposition.txt'); $in->seek($number1); while($kw = trim(fgets($in)))

我想加载一个包含数字的文件,并将其用作数字(而不是字符串)。有什么解决办法吗?我得到的错误

对非对象调用成员函数seek() 我的PHP代码

$in = fopen('emails.txt','r');
$out = fopen('currentposition.txt', 'r+');
$pos = file_get_contents('currentposition.txt');
$in->seek($number1); 
while($kw = trim(fgets($in))) {
    //my code
    $position = $in->current();
    fwrite($out, $position);
}

fclose($in);
fclose($out);
档案

$ cat /tmp/ll
9
剧本:

<?php

$x = file_get_contents("/tmp/ll");
echo $x + 10;
?>

输出:
19

使用
$pos
而不是
$number1

答案就在您收到的错误消息中:这意味着您正在调用方法,即对象的成员函数,用于非对象的对象(在面向对象编程的意义上)
$in
不是类的对象,只是
资源

编辑

现在我正确理解了您想要完成的任务,请尝试以下操作:

//在写入(覆盖)时使用w-截断,b-以防在Windows上运行此操作
$recordFile=fopen('currentposition.txt','wbr');
$emailsFile=fopen('emails.txt','r');
$pos=修剪(fgets($recordFile));
//如果是第一次,并且currentposition.txt中没有0
如果(!isset($pos))
$pos=0;
//设置指针
fseek($emailsFile,$pos);
//阅读内容;
$content=fread($emailsFile,filesize($emailsFile));
//获取文件指针的当前位置
$pos=ftell($emailsFile);
//写入文件中的最后一个位置
fwrite($recordFile,$pos);
fclose($recordFile);
fclose($emailsFile);
老实说,这应该是可行的,但还没有经过测试。我希望你能大致了解情况

添加

上面的代码一次将电子邮件文件的所有内容读入一个(字符串)变量。然后可以使用
\n
作为分隔符拆分它,类似于
$allEmails=split('\n',$content)
并将电子邮件放入一个数组中,您可以在其中循环。 无论如何,这里是相同的代码,但是使用
while
循环,也就是说,逐行读取文件-对于非常大的文件(MB)是很好的


你能发布几行你的
emails.txt
&
currentposition.txt
filesAFAIK
seek()
SplFileObject
对象的一种方法,但是
fopen()
返回一个资源,而不是一个对象。fseek()只设置引用文件的位置指示器,它不是一个搜索函数。考虑一下我使用FSETH的手册,因为这里有人建议我使用它。emails.txt包含一个200mb的emails,当前位置是空白的txt文件。实际上,我想读取emails.txt文件,当我停止脚本时,我希望能够从脚本停止的那一行继续读取该文件……我尝试了这个方法,但脚本没有循环……我已经添加了您给我的前10行代码在while外部和while内部的其他代码……好的,我添加了第二种方法来执行相同的操作。我并不是想听话,但也许你也应该考虑更多的编程课程/教程——网上都有。当然,除非你必须参加的课程中有一些“愚蠢”的家庭作业:)
// use w - to truncate when writing (overwrite), b - in case you ever run this on Windows
$recordFile = fopen('currentposition.txt', 'wbr');
$emailsFile = fopen('emails.txt', 'r');
$pos = trim(fgets($recordFile));
// if first time and there was no 0 in the currentposition.txt
if(!isset($pos))
    $pos = 0;

//set the pointer
fseek($emailsFile, $pos);


// while end of file is not reached
while(!feof($emailsFile)){
    // read one line of the file and remove leading and trailing blanks
    $kw = trim(fgets($emailsFile));
    // .... do something with the line you've read
    // get the current position of the file pointer
    $pos = ftell($emailsFile);    
    // you don't need to write the position in the $recordFile every loop!
    // saving it in $pos is just enough
}

// write the last position in the file
fwrite($recordFile, $pos);

fclose($recordFile);
fclose($emailsFile);