iT邦幫忙

0

php 讀記事本的數字與寫入換行

php
  • 分享至 

  • twitterImage

各位大大好:

我想要確認產生的亂數不會重複,所以想把產生的亂數寫入記事本
不知道為什麼寫到第三個數字,他沒辦法自動換行? 且有測試過若輸入相同的數字進去,程式也無法比對,產生警告說有重複的數字,並重新產生亂數(只有第一行數字可比對到重複..)

711post.txt檔
9835
26198 15001

function writeMsg($a) {
          $myfile = fopen("711post.txt", "a+");
         if ($a!=fgets($myfile)){
         
         $b=$a."\n";
         fwrite($myfile,$b);
         fclose($myfile);
         echo "<script>alert('警告:確認之後跳頁');</script>";
         echo $b;
         
         }
         
         else{
         echo "<script>alert('警告:數字重複');</script>";
         $g=substr(rand(),0,5);
         writeMsg($g);
         }

       }
        $g=substr(rand(),0,5);
        writeMsg($g);
weiclin iT邦高手 4 級 ‧ 2019-05-13 13:30:35 檢舉
你這個還要考慮一下同時有兩隻程式執行寫入相同數字的問題喔
mayyola iT邦研究生 2 級 ‧ 2019-05-15 08:49:03 檢舉
w大您好:有回在回答下面了,不知道是不是您說的那樣 謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
froce
iT邦大師 1 級 ‧ 2019-05-12 00:57:51

1.windows上記事本的換行是"\r\n"。你換個正常的文字編輯器就ok了。(vs code之類的)
2.fget()一次只會讀一行,你連迴圈都沒有,永遠在比對第一行。

https://blog.longwin.com.tw/2008/08/php-fgets-fread-diff-example-2008/

mayyola iT邦研究生 2 級 ‧ 2019-05-12 13:26:51 檢舉

修改如下

mayyola iT邦研究生 2 級 ‧ 2019-05-12 14:38:06 檢舉

f大好:大概有改寫跟測試..不知道第三次有重複..程式是否還可正確判斷

59062-98345 沒有重複
59062-59062 有重複 #開始產生新亂數writeMsgagain($a)
22222-98345 沒有重複1
22222-59062 沒有重複1
22222-22222 有重複 再跳writeMsgagain($a)
11111-98345 沒有重複1
11111-59062 沒有重複1
11111-22222 沒有重複1
11111-沒有重複 #不曉得為什麼會多出一行?
警告:確認之後跳頁

711post.txt
98345
59062
22222
11111 #多新一行

function writeMsgagain($a) {
       
              $myfile = fopen("711post.txt", "a+");
          
         while (!feof($myfile)){
                   
         $buffer=fgets($myfile);
         $buffer = str_replace(array("\r\n"), '',$buffer);       
         if (strcmp($buffer, $a)== 0){
         
           echo "<script>alert('".$a."-".$buffer."有重複');</script>";   
           $c=(substr(rand(10000,99999),0,5));  
           $a=writeMsgagain($c);               
           break;
                   }
         
          else{
                                      
               echo "<script>alert('".$a."-".$buffer."沒有重複1');</script>";
               continue;
                    }                   
               }
        
                return  $a;
       }
            
       function writeMsg($a) {
       
       $myfile = fopen("711post.txt", "a+");
          
         while (!feof($myfile)){
                   
         $buffer=fgets($myfile);
         $buffer = str_replace(array("\r\n"), '',$buffer);       
         if (strcmp($buffer, $a)== 0){        
           echo "<script>alert('".$a."-".$buffer."有重複');</script>";   
           $c=(substr(rand(10000,99999),0,5));  
                $a=writeMsgagain($c); //若$c改為22222驗證
                break; 
                 }
   
                    else{
                                      
                       echo "<script>alert('".$a."-".$buffer."沒有重複');</script>";
                       continue;
 
                    }
                    
       }
       
       $b=$a."\r\n";
       fwrite($myfile,$b);
       fclose($myfile);
       echo "<script>alert('警告:確認之後跳頁');</script>";
       
       }
       
       $g=59062;
       writeMsg($g);


我要發表回答

立即登入回答