PHP file Operations-add data from other files to this file

Source: Internet
Author: User

The content of this article is PHP file operation-Add the data of other files to this file, now share to everyone, the need for friends can refer to

GitHub source
In fact our program is based on the following questions written by the answer:

There are two text files A.txt B.txt
A.txt 30 million lines, UserID unique, UserID and username are separated by spaces, as follows:
UserID username
1 Yi
2 ER
3 San
... ...
B.txt 30 million lines, UserID unique, UserID and realname are separated by spaces, as follows:
UserID Realname
11
22
33
... ...
Please write a code that will B.txt the userid corresponding to the username in A.txt, fill in the third column of B.txt, and give the time complexity.

In our program, the default two file row data is one by one corresponding, that is, the nth row of a data corresponding to the nth row of B data, so that our program's time complexity is O (n).

But in the actual operation, will certainly encounter two files data row is not one by one corresponding case, so I only think of the simplest time complexity for O (n^2) operation, do not know there is no better algorithm to solve the problem.

If a file is read out and constructed as a data structure with an O (1) or O (logn) query, it should be complex to be O (n) or O (Nlogn), but how to deal with a file that is too large to be built, and how to use the database B-tree indexing approach?

<?phpheader ("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
In fact our program is based on the following questions written by the answer:

There are two text files A.txt B.txt
A.txt 30 million lines, UserID unique, UserID and username are separated by spaces, as follows:
UserID username
1 Yi
2 ER
3 San
... ...
B.txt 30 million lines, UserID unique, UserID and realname are separated by spaces, as follows:
UserID Realname
11
22
33
... ...
Please write a code that will B.txt the userid corresponding to the username in A.txt, fill in the third column of B.txt, and give the time complexity.

In our program, the default two file row data is one by one corresponding, that is, the nth row of a data corresponding to the nth row of B data, so that our program's time complexity is O (n).

But in the actual operation, will certainly encounter two files data row is not one by one corresponding case, so I only think of the simplest time complexity for O (n^2) operation, do not know there is no better algorithm to solve the problem.

If a file is read out and constructed as a data structure with an O (1) or O (logn) query, it should be complex to be O (n) or O (Nlogn), but how to deal with a file that is too large to be built, and how to use the database B-tree indexing approach?

<?phpheader ("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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.