Home > Article > Backend Development > PHP file operation-add data from other files to this file
The content of this article is php file operation-add the data of other files to this file, now share it with everyone, friends in need can refer to it
GitHub source code
In fact, our The program writes answers based on the following questions:
There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as shown below:
userid username
1 yi yi
2 er er
3 san
… …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1 ##userid realname
1 Go to the third column of B.txt and give the time complexity.
In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).
<?php header("content-type:text/html;charset=utf-8"); function decodeLine(string $lineData, string $delimiter = null) { if (is_null($delimiter)) { $delimiter = ' '; } return explode($delimiter, $lineData); } function encodeLine(array $dataList, string $delimiter = null) { if (is_null($delimiter)) { $delimiter = ' '; } return implode($delimiter, $dataList); }$testA = fopen('./TestData/FileOperation/testA.txt', 'r');$testB = fopen('./TestData/FileOperation/testB.txt', 'r+');$tmpFile = tmpfile();//while (($bBuffer = fgets($testB)) != false) { $bList = decodeLine(trim($bBuffer, "\n\r")); $tmpList = $bList; if (($aBuffer = fgets($testA)) != false) { $aList = decodeLine(trim($aBuffer, "\n\r")); if ($aList[0] == $bList[0]) { $strEncoding = mb_detect_encoding($aList[1], ['ASCII', 'UTF-8', 'GB2312']); $resStr = mb_convert_encoding($aList[1], 'UTF-8', $strEncoding); array_push($tmpList, $resStr."\n"); } } fwrite($tmpFile, encodeLine($tmpList)); } rewind($tmpFile); rewind($testB);while (!feof($tmpFile)) { $tmpBuffer = fread($tmpFile, 1024); fwrite($testB, $tmpBuffer); }fclose($tmpFile);fclose($testA);fclose($testB);GitHub Source Code
In fact, our program is based on the following issues Written answer:
There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as follows:
1 yi yi
2 er
3 san
… …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1 一
2 二
3 三
… …
Please write a piece of code to find the username corresponding to the userid in B.txt in A.txt and fill it in B.txt The third column gives the time complexity.
In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).
<?php header("content-type:text/html;charset=utf-8"); function decodeLine(string $lineData, string $delimiter = null) { if (is_null($delimiter)) { $delimiter = ' '; } return explode($delimiter, $lineData); } function encodeLine(array $dataList, string $delimiter = null) { if (is_null($delimiter)) { $delimiter = ' '; } return implode($delimiter, $dataList); }$testA = fopen('./TestData/FileOperation/testA.txt', 'r');$testB = fopen('./TestData/FileOperation/testB.txt', 'r+');$tmpFile = tmpfile();//while (($bBuffer = fgets($testB)) != false) { $bList = decodeLine(trim($bBuffer, "\n\r")); $tmpList = $bList; if (($aBuffer = fgets($testA)) != false) { $aList = decodeLine(trim($aBuffer, "\n\r")); if ($aList[0] == $bList[0]) { $strEncoding = mb_detect_encoding($aList[1], ['ASCII', 'UTF-8', 'GB2312']); $resStr = mb_convert_encoding($aList[1], 'UTF-8', $strEncoding); array_push($tmpList, $resStr."\n"); } } fwrite($tmpFile, encodeLine($tmpList)); } rewind($tmpFile); rewind($testB);while (!feof($tmpFile)) { $tmpBuffer = fread($tmpFile, 1024); fwrite($testB, $tmpBuffer); }fclose($tmpFile);fclose($testA);fclose($testB);Related recommendations:
Get the directory for PHP file operations Files and methods of calculating relative paths
patch files PHP file operations to implement code sharing
The above is the detailed content of PHP file operation-add data from other files to this file. For more information, please follow other related articles on the PHP Chinese website!