Programming Basics

  Home arrow Programming Basics arrow Page 5 - Working with text files
PROGRAMMING BASICS

Working with text files
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2004-01-06

    Table of Contents:
  • Working with text files
  • Opening a text file
  • Writing data into a text file
  • Closing a text file
  • Reading from a text file
  • Deleting a file
  • Other functions regarding text files
  • An example
  • Advanced examples
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Working with text files - Reading from a text file


    (Page 5 of 10 )

    The most common way of reading from a text file is to use the fread() function though there are many other ways to read as well. We will discuss about other functions in a later chapter of this tutorial. To get a reasonable understanding of this part, we'll talk about fread() here.

    To read a file, there should be a variable with a pointer to a opened file which we mentioned in last few chapters. This is the common syntax of this function.

    <?php
    $content = fread( $fp, filesize( $filename ) );
    ?>

    The first parameter is, as usual, a pointer to a file. Second parameter is the number of bytes that should be read in by the function. Usually, when we need to read the whole file, we use the filesize() function to help this matter. It will return the total bytes of the given file. So, then fread() gets its second parameter as total number of bytes. It places the content of the whole file pointed by $fp in the $content variable. You may use $content to do any type of processing as now it has the all ingredients of the file.

    For more help: http://us2.php.net/manual/en/function.fread.php

    More Programming Basics Articles
    More By Codewalkers

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap