Jump to content

[SOLVED] Can you read individual bytes from a flat file?


Meissa

Recommended Posts

This is probably really stupid...  maybe I'm wording the question wrong in google

 

Example:

I have a flatfile that is 4 bytes long:  1234

 

I want to assign four different variables:

 

$a = flatfile(byte1)  (I want it to equal "1")

$b = flatfile(byte2)  (I want it to equal "2")

$c = flatfile(byte3)  (so forth, so on...)

$d = flatfile(byte4)

 

I can't figure out the syntax...  is there a way to do this, or is this really too simple to have ever been practical?

 

 

Meissa =/

Link to comment
Share on other sites

to read a flat file one byte at a time...

<?php
        /*open file for reading*/
        $fp = fopen('your/directory/file.txt', 'r');

        /*while still reading the file*/
        while(!feof($fp)){
                $character = fgets($fp, 2);
                echo $character ."<br />\n";
        }
?>

 

your situation may call for this, but i wouldn't recommend assigning a new variable for every byte in your flat file. if i were you, i'd store them all into an array.

 

<?php
        /*set array*/
        $array = array();

        /*open file for reading*/
        $fp = fopen('your/directory/file.txt', 'r');

        /*while still reading the file*/
        while(!feof($fp)){
                $array[] = fgets($fp, 2);
        }

        print_r($array);
?>

 

 

Link to comment
Share on other sites

Here's what I'm trying to do.. sorry if I don't write this properly:

 

 

<?php

          /*My homemade counter*/

          $countertxt = "counter.txt";                         
          $fr = fopen($countertxt, 'r');
          $count = fread($fr, filesize($countertxt));       //reads the current value of the flat file
          $count = $count + 1;                             //increments the value by 1
          $fo = fopen($countertxt, 'w');                       
          fwrite($fo, $count);                             //writes the new value to the flat file
          echo $count;                                     //everything at this point works fine

          /*Here's where I'm running into trouble*/
         
          /*I want to make the page display a .GIF file according to the number that is stored in the flat file*/
          $a = $count[0];
          $b = $count[1];
          $c = $count[2];
          $d = $count[3];

          echo "<img src='$a.GIF'>";
          echo "<img src='$b.GIF'>";
          echo "<img src='$c.GIF'>";
          echo "<img src='$d.GIF'>";

?>

 

 

I'm unable to read the variable $count one byte at a time

 

However, if I were to assign the value like this:

 

$count = '1234';

 

It works properly... for some reason it won't treat $count like a variable with 4 numbers... probably because $count is associated with opening files... 

 

Any thoughts?

 

 

 

 

 

 

Link to comment
Share on other sites

Try This,

 

Again I am a newbie, sure someone has a better way, but seems you need to convert it from a number to a string.

 

<?php

          /*My homemade counter*/

          $countertxt = "counter.txt";                         
          $fr = fopen($countertxt, 'r');
          $count = fread($fr, filesize($countertxt));       //reads the current value of the flat file
          $count = $count + 1;                             //increments the value by 1
          $fo = fopen($countertxt, 'w');                       
          fwrite($fo, $count);                             //writes the new value to the flat file
          echo $count;                                     //everything at this point works fine
	  
		$data = strval($count);   /// need to convert the number to a string to use the method below

          /*Here's where I'm running into trouble*/
         
          /*I want to make the page display a .GIF file according to the number that is stored in the flat file*/
          $a = $data[0];
          $b = $data[1];
          $c = $data[2];
          $d = $data[3];

          echo "<img src='$a.GIF'>";
          echo "<img src='$b.GIF'>";
          echo "<img src='$c.GIF'>";
          echo "<img src='$d.GIF'>";

?>

 

This works

 

Gaz

Link to comment
Share on other sites

i'm sorry i misunderstood the issue. try this:

<?php
        /*set array*/
        $array = array();

        /*open file for reading*/
        $fp = fopen('your/directory/file.txt', 'r');

        /*store each byte into an index in the array*/
        while(!feof($fp)){
                $array[] = fgets($fp, 2);
        }

        /*print each index in the array as an image name*/
        foreach($array as $img){
                echo "<img src=\"{$img}.GIF\"><br />\n";
        }
?>

Link to comment
Share on other sites

Melissa, thanks

 

Newer code here

 

<?php

          /*My homemade counter*/

          $countertxt = "counter.txt";                         
          $fr = fopen($countertxt, 'r');
          $count = fread($fr, filesize($countertxt));       //reads the current value of the flat file
          $count = $count + 1;                             //increments the value by 1
          $fo = fopen($countertxt, 'w');                       
          fwrite($fo, $count);                             //writes the new value to the flat file
          echo $count;                                     //everything at this point works fine
	  
	  
	  if ($count < 10){                   //
	  $count = "0".$count;
	  }
	   if ($count < 100){
	  $count = "0".$count;            //code here added to add prevailing zeros before number reaches 1000, to 
	  }                                // display 0004, 0014, 0114 etc.
	   if ($count < 1000){
	  $count = "0".$count;
	  }                                //

		$data = strval($count);   /// need to convert the number to a string to use the method below

          /*Here's where I'm running into trouble*/
         

          /*I want to make the page display a .GIF file according to the number that is stored in the flat file*/
          $a = $data[0];
          $b = $data[1];
          $c = $data[2];
          $d = $data[3];

          echo "<img src='$a.GIF'>";
          echo "<img src='$b.GIF'>";
          echo "<img src='$c.GIF'>";
          echo "<img src='$d.GIF'>";

?>

 

Problem when display a count 0f 0014, etc, would display as 14-- as there the first two characters hope you understand what im on about.

 

Gaz

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.