Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4116354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:41:52+00:00 2026-05-20T22:41:52+00:00

I am trying to extract contents from a txt file. This file is dynamic

  • 0

I am trying to extract contents from a txt file. This file is dynamic because data keeps appending to it everytime the loop is executed.
Inside this loop rests my logic for extracting contents from file, as follows…

$length = filesize($filename);  
fseek($fd,$previousLength);  
$contents = fread($fd,(($length - $previousLength)));  
$previousLength = $length;  

i.e, I AM trying to read only the data that got appended, in the last loop … and not the data that was previously written.
EXAMPLE… A txt adds ONE everytime a loop is run.. i.e consider

114134, 144, 1443, 1433 ... 
(n of these written every once in loop ) ... 

If I read n values , say

114134, 144 ... 

in the first loop …

next time, I need to read only

1443, 1443 and NOT 114134, 144 ....

fread() fails miserably here ,and fseek doesn’t help ( ref. my code above) …

I DON”T KNOW WHY !! help needed asap ..

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:41:52+00:00Added an answer on May 20, 2026 at 10:41 pm

    If you have opened the file in append mode then the man page for fseek says:

    If you have opened the file in append (a or a+) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek() will be undefined.

    The following code has a few modifications. I had a few problems with the length – fread does require it, but i decided to use fgets to avoid it. This would stop on newline characters but has the handy feature of reading the entire remaining contents of the file otherwise. There may be a better way of doing this, but this does work.

    <?php
    $filename = 'loopFile.txt';
    $previousLength = 0;
    $n = 0;
    
    $fw = fopen($filename, 'a+');
    $fr = fopen($filename, 'r');
    
    for ($i=0; $i < 15; $i++)
    {
       // Put a random number of numbers into the file.
       $numberOfNumbers = rand(0, 5);
    
       for ($writeCount = 0; $writeCount < $numberOfNumbers; $writeCount++)
       {
          fwrite($fw, $i . '_' . $n++ . ', ');
       }
    
       fseek($fr, $previousLength);  
       $contents = fgets($fr);
    
       if (!empty($contents))
       {
          echo 'On iteration: ' . $i . ' read: ' . $contents . "\n";
       }
       else
       {
          echo 'On iteration: ' . $i . ' no new data appended to file.' . "\n";
       }
    
       $previousLength += strlen($contents);  
    }
    
    fclose($fw);
    fclose($fr);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to extract some files from a jar-file downloaded using java-webstart. below code
I'm trying to keep linebreaks reading from a txt file when I print the
I am trying to extract table content from a html file using HTML::TableExtract. My
I’m working with java Scanner trying to extract product information from a text file
I am trying to parse a bit of data from an HTML file, but
I am trying to extract the content from a gpx file. The problem is
I'm trying to get data from a webpage that serves a XML file periodically
I am trying to extract images from a PDF file. I found an example
I am trying to extract blocks of JSON data from a data stream in
I am trying to extract a single word from a content editable div at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.