Problem reading TXT file

Source: Internet
Author: User
One. How do I write the content to the first line of the TXT file each time.

Two. How do I read the last line of the TXT file?


Reply to discussion (solution)

One:

$handle  =  fopen (' a.txt ',  ' r+ '); Rewind ($handle); Fwrite ($handle, "This is the first line \ r \ n"); fclose ($handle);


Two:

$handle  =  fopen (' a.txt ',  ' r+ '); $arr =array (); while ($line =fgets ($handle)) {    $arr []= $line;} Fclose ($handle); Echo End ($arr);

One. That write txt first line to the original content of the TXT file is cleared, I want to keep the original content, new content inserted into the first line.

$str = "This is the first line \ r \ n";  $handle  =  fopen (' a.txt ',  ' r+ ');   while ($line =fgets ($handle)) {    $str. = $line;   }   Fclose ($handle);   File_put_contents (' A.txt ', $str);

It seems that there is no direct function to achieve, can only loop out the data, thank you

  • 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.